Python: Parent Class not recognizing __init__ of its subclasses












0















I have been working with inheritance, but I got confused as to why my parent class is not recognizing the init of my sub classes. Here is an example of my code that is giving me none when run, when it shouldn't.



class Coins(object):
def make_change(self, amount):
change =
for coin in (self._coins):
change.append(amount // coin)
amount = amount - change[-1] * coin
class US_Coins(Coins):
def __init__(self):
self._coins = [50, 25, 10, 5, 1]
class Metric_Coins(Coins):
def __init__(self):
self._coins = [50, 20, 10, 5, 2, 1]
metric = Metric_Coins()
us = US_Coins()
print(us.make_change(73))
print(metric.make_change(73))
coins = Coins()
print(coins.make_change(27))









share|improve this question


















  • 1





    Inheritance works upwards, not downwards. Meaning Metric_Coins will inherit properties from Coins. But Coins won't inherit anything from anyone. Meaning Coins won't get any values or properties from any of the other classes. One such variable is self._coins which is only created in two of the classes, but never defined in Coins. As @antona pointed out correctly. this shows a basic concept of how this works.

    – Torxed
    Nov 20 '18 at 6:33








  • 2





    You should add a return change to your make_changemethod so that it gives you an output. Also, only subclasses can get methods from their parents and not the other way around.

    – N Chauhan
    Nov 20 '18 at 6:43











  • Can you clarify what your problem is? A parent class is not meant to do anything with methods of its subclasses. Even in your case, it would not make sense if Coins were to use __init__ of its subclasses - the two are not compatible. Would Coins()._coins be [50, 25, 10, 5, 1] or [50, 20, 10, 5, 2, 1]?

    – MisterMiyagi
    Nov 20 '18 at 10:17


















0















I have been working with inheritance, but I got confused as to why my parent class is not recognizing the init of my sub classes. Here is an example of my code that is giving me none when run, when it shouldn't.



class Coins(object):
def make_change(self, amount):
change =
for coin in (self._coins):
change.append(amount // coin)
amount = amount - change[-1] * coin
class US_Coins(Coins):
def __init__(self):
self._coins = [50, 25, 10, 5, 1]
class Metric_Coins(Coins):
def __init__(self):
self._coins = [50, 20, 10, 5, 2, 1]
metric = Metric_Coins()
us = US_Coins()
print(us.make_change(73))
print(metric.make_change(73))
coins = Coins()
print(coins.make_change(27))









share|improve this question


















  • 1





    Inheritance works upwards, not downwards. Meaning Metric_Coins will inherit properties from Coins. But Coins won't inherit anything from anyone. Meaning Coins won't get any values or properties from any of the other classes. One such variable is self._coins which is only created in two of the classes, but never defined in Coins. As @antona pointed out correctly. this shows a basic concept of how this works.

    – Torxed
    Nov 20 '18 at 6:33








  • 2





    You should add a return change to your make_changemethod so that it gives you an output. Also, only subclasses can get methods from their parents and not the other way around.

    – N Chauhan
    Nov 20 '18 at 6:43











  • Can you clarify what your problem is? A parent class is not meant to do anything with methods of its subclasses. Even in your case, it would not make sense if Coins were to use __init__ of its subclasses - the two are not compatible. Would Coins()._coins be [50, 25, 10, 5, 1] or [50, 20, 10, 5, 2, 1]?

    – MisterMiyagi
    Nov 20 '18 at 10:17
















0












0








0


1






I have been working with inheritance, but I got confused as to why my parent class is not recognizing the init of my sub classes. Here is an example of my code that is giving me none when run, when it shouldn't.



class Coins(object):
def make_change(self, amount):
change =
for coin in (self._coins):
change.append(amount // coin)
amount = amount - change[-1] * coin
class US_Coins(Coins):
def __init__(self):
self._coins = [50, 25, 10, 5, 1]
class Metric_Coins(Coins):
def __init__(self):
self._coins = [50, 20, 10, 5, 2, 1]
metric = Metric_Coins()
us = US_Coins()
print(us.make_change(73))
print(metric.make_change(73))
coins = Coins()
print(coins.make_change(27))









share|improve this question














I have been working with inheritance, but I got confused as to why my parent class is not recognizing the init of my sub classes. Here is an example of my code that is giving me none when run, when it shouldn't.



class Coins(object):
def make_change(self, amount):
change =
for coin in (self._coins):
change.append(amount // coin)
amount = amount - change[-1] * coin
class US_Coins(Coins):
def __init__(self):
self._coins = [50, 25, 10, 5, 1]
class Metric_Coins(Coins):
def __init__(self):
self._coins = [50, 20, 10, 5, 2, 1]
metric = Metric_Coins()
us = US_Coins()
print(us.make_change(73))
print(metric.make_change(73))
coins = Coins()
print(coins.make_change(27))






python inheritance






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 6:15









John DoeJohn Doe

92




92








  • 1





    Inheritance works upwards, not downwards. Meaning Metric_Coins will inherit properties from Coins. But Coins won't inherit anything from anyone. Meaning Coins won't get any values or properties from any of the other classes. One such variable is self._coins which is only created in two of the classes, but never defined in Coins. As @antona pointed out correctly. this shows a basic concept of how this works.

    – Torxed
    Nov 20 '18 at 6:33








  • 2





    You should add a return change to your make_changemethod so that it gives you an output. Also, only subclasses can get methods from their parents and not the other way around.

    – N Chauhan
    Nov 20 '18 at 6:43











  • Can you clarify what your problem is? A parent class is not meant to do anything with methods of its subclasses. Even in your case, it would not make sense if Coins were to use __init__ of its subclasses - the two are not compatible. Would Coins()._coins be [50, 25, 10, 5, 1] or [50, 20, 10, 5, 2, 1]?

    – MisterMiyagi
    Nov 20 '18 at 10:17
















  • 1





    Inheritance works upwards, not downwards. Meaning Metric_Coins will inherit properties from Coins. But Coins won't inherit anything from anyone. Meaning Coins won't get any values or properties from any of the other classes. One such variable is self._coins which is only created in two of the classes, but never defined in Coins. As @antona pointed out correctly. this shows a basic concept of how this works.

    – Torxed
    Nov 20 '18 at 6:33








  • 2





    You should add a return change to your make_changemethod so that it gives you an output. Also, only subclasses can get methods from their parents and not the other way around.

    – N Chauhan
    Nov 20 '18 at 6:43











  • Can you clarify what your problem is? A parent class is not meant to do anything with methods of its subclasses. Even in your case, it would not make sense if Coins were to use __init__ of its subclasses - the two are not compatible. Would Coins()._coins be [50, 25, 10, 5, 1] or [50, 20, 10, 5, 2, 1]?

    – MisterMiyagi
    Nov 20 '18 at 10:17










1




1





Inheritance works upwards, not downwards. Meaning Metric_Coins will inherit properties from Coins. But Coins won't inherit anything from anyone. Meaning Coins won't get any values or properties from any of the other classes. One such variable is self._coins which is only created in two of the classes, but never defined in Coins. As @antona pointed out correctly. this shows a basic concept of how this works.

– Torxed
Nov 20 '18 at 6:33







Inheritance works upwards, not downwards. Meaning Metric_Coins will inherit properties from Coins. But Coins won't inherit anything from anyone. Meaning Coins won't get any values or properties from any of the other classes. One such variable is self._coins which is only created in two of the classes, but never defined in Coins. As @antona pointed out correctly. this shows a basic concept of how this works.

– Torxed
Nov 20 '18 at 6:33






2




2





You should add a return change to your make_changemethod so that it gives you an output. Also, only subclasses can get methods from their parents and not the other way around.

– N Chauhan
Nov 20 '18 at 6:43





You should add a return change to your make_changemethod so that it gives you an output. Also, only subclasses can get methods from their parents and not the other way around.

– N Chauhan
Nov 20 '18 at 6:43













Can you clarify what your problem is? A parent class is not meant to do anything with methods of its subclasses. Even in your case, it would not make sense if Coins were to use __init__ of its subclasses - the two are not compatible. Would Coins()._coins be [50, 25, 10, 5, 1] or [50, 20, 10, 5, 2, 1]?

– MisterMiyagi
Nov 20 '18 at 10:17







Can you clarify what your problem is? A parent class is not meant to do anything with methods of its subclasses. Even in your case, it would not make sense if Coins were to use __init__ of its subclasses - the two are not compatible. Would Coins()._coins be [50, 25, 10, 5, 1] or [50, 20, 10, 5, 2, 1]?

– MisterMiyagi
Nov 20 '18 at 10:17














1 Answer
1






active

oldest

votes


















1














You need to define self._coins field in Coins class. Without it, function make_change cannot be executed because it evokes field that does not exist in this parent class (for coin in (self._coins)).



Edited: in order to achieve your goal you need to create a field inside Coins class:



class Coins(object):

def __init__self():
self._coins =

def make_change(self, amount):
change =
for coin in (self._coins):
change.append(amount // coin)
amount = amount - change[-1] * coin


class US_Coins(Coins):

def __init__(self):
self._coins = [50, 25, 10, 5, 1]


class Metric_Coins(Coins):

def __init__(self):
self._coins = [50, 20, 10, 5, 2, 1]


metric = Metric_Coins()
us = US_Coins()
print(us.make_change(73))
print(metric.make_change(73))
coins = Coins()
print(coins.make_change(27))


Now you are able to operate with make_change method on the us and metric; and also on coins object.






share|improve this answer


























  • Thank you, is there any way to account for the changing self._coins ([50, 25, 10, 5, 1] and [50, 20, 10, 5, 2, 1]) in the Coins class?

    – John Doe
    Nov 20 '18 at 7:03













  • @JohnDoe made update

    – artona
    Nov 20 '18 at 10:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387254%2fpython-parent-class-not-recognizing-init-of-its-subclasses%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You need to define self._coins field in Coins class. Without it, function make_change cannot be executed because it evokes field that does not exist in this parent class (for coin in (self._coins)).



Edited: in order to achieve your goal you need to create a field inside Coins class:



class Coins(object):

def __init__self():
self._coins =

def make_change(self, amount):
change =
for coin in (self._coins):
change.append(amount // coin)
amount = amount - change[-1] * coin


class US_Coins(Coins):

def __init__(self):
self._coins = [50, 25, 10, 5, 1]


class Metric_Coins(Coins):

def __init__(self):
self._coins = [50, 20, 10, 5, 2, 1]


metric = Metric_Coins()
us = US_Coins()
print(us.make_change(73))
print(metric.make_change(73))
coins = Coins()
print(coins.make_change(27))


Now you are able to operate with make_change method on the us and metric; and also on coins object.






share|improve this answer


























  • Thank you, is there any way to account for the changing self._coins ([50, 25, 10, 5, 1] and [50, 20, 10, 5, 2, 1]) in the Coins class?

    – John Doe
    Nov 20 '18 at 7:03













  • @JohnDoe made update

    – artona
    Nov 20 '18 at 10:12
















1














You need to define self._coins field in Coins class. Without it, function make_change cannot be executed because it evokes field that does not exist in this parent class (for coin in (self._coins)).



Edited: in order to achieve your goal you need to create a field inside Coins class:



class Coins(object):

def __init__self():
self._coins =

def make_change(self, amount):
change =
for coin in (self._coins):
change.append(amount // coin)
amount = amount - change[-1] * coin


class US_Coins(Coins):

def __init__(self):
self._coins = [50, 25, 10, 5, 1]


class Metric_Coins(Coins):

def __init__(self):
self._coins = [50, 20, 10, 5, 2, 1]


metric = Metric_Coins()
us = US_Coins()
print(us.make_change(73))
print(metric.make_change(73))
coins = Coins()
print(coins.make_change(27))


Now you are able to operate with make_change method on the us and metric; and also on coins object.






share|improve this answer


























  • Thank you, is there any way to account for the changing self._coins ([50, 25, 10, 5, 1] and [50, 20, 10, 5, 2, 1]) in the Coins class?

    – John Doe
    Nov 20 '18 at 7:03













  • @JohnDoe made update

    – artona
    Nov 20 '18 at 10:12














1












1








1







You need to define self._coins field in Coins class. Without it, function make_change cannot be executed because it evokes field that does not exist in this parent class (for coin in (self._coins)).



Edited: in order to achieve your goal you need to create a field inside Coins class:



class Coins(object):

def __init__self():
self._coins =

def make_change(self, amount):
change =
for coin in (self._coins):
change.append(amount // coin)
amount = amount - change[-1] * coin


class US_Coins(Coins):

def __init__(self):
self._coins = [50, 25, 10, 5, 1]


class Metric_Coins(Coins):

def __init__(self):
self._coins = [50, 20, 10, 5, 2, 1]


metric = Metric_Coins()
us = US_Coins()
print(us.make_change(73))
print(metric.make_change(73))
coins = Coins()
print(coins.make_change(27))


Now you are able to operate with make_change method on the us and metric; and also on coins object.






share|improve this answer















You need to define self._coins field in Coins class. Without it, function make_change cannot be executed because it evokes field that does not exist in this parent class (for coin in (self._coins)).



Edited: in order to achieve your goal you need to create a field inside Coins class:



class Coins(object):

def __init__self():
self._coins =

def make_change(self, amount):
change =
for coin in (self._coins):
change.append(amount // coin)
amount = amount - change[-1] * coin


class US_Coins(Coins):

def __init__(self):
self._coins = [50, 25, 10, 5, 1]


class Metric_Coins(Coins):

def __init__(self):
self._coins = [50, 20, 10, 5, 2, 1]


metric = Metric_Coins()
us = US_Coins()
print(us.make_change(73))
print(metric.make_change(73))
coins = Coins()
print(coins.make_change(27))


Now you are able to operate with make_change method on the us and metric; and also on coins object.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 14:48

























answered Nov 20 '18 at 6:18









artonaartona

71247




71247













  • Thank you, is there any way to account for the changing self._coins ([50, 25, 10, 5, 1] and [50, 20, 10, 5, 2, 1]) in the Coins class?

    – John Doe
    Nov 20 '18 at 7:03













  • @JohnDoe made update

    – artona
    Nov 20 '18 at 10:12



















  • Thank you, is there any way to account for the changing self._coins ([50, 25, 10, 5, 1] and [50, 20, 10, 5, 2, 1]) in the Coins class?

    – John Doe
    Nov 20 '18 at 7:03













  • @JohnDoe made update

    – artona
    Nov 20 '18 at 10:12

















Thank you, is there any way to account for the changing self._coins ([50, 25, 10, 5, 1] and [50, 20, 10, 5, 2, 1]) in the Coins class?

– John Doe
Nov 20 '18 at 7:03







Thank you, is there any way to account for the changing self._coins ([50, 25, 10, 5, 1] and [50, 20, 10, 5, 2, 1]) in the Coins class?

– John Doe
Nov 20 '18 at 7:03















@JohnDoe made update

– artona
Nov 20 '18 at 10:12





@JohnDoe made update

– artona
Nov 20 '18 at 10:12




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387254%2fpython-parent-class-not-recognizing-init-of-its-subclasses%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings