Python : Multiple inheritance problems
I'm a little confused about an inheritance problem in python.
I know the following example is "stupid" but I simplify my initial problem.
Imagine we have 3 classes
class testT(object):
def check(self, str):
return "t" in str
class testTE(testT):
def check(self, str):
return "e" in str
class testTES(testTE):
def check(self, str):
return "s" in str
And I would like an output like :
>>> print testTES().check("test")
>>> True (Because string "test" contains "s","e" and "t" characters)
>>> print testTES().check("dog")
>>> False
>>> print testTES().check("dogs")
>>> False (Contains a "s" but no "e" and no "t")
>>> print testTE().check("tuple")
>>> True (Contains "e" and "t")
How can I implement this behavior ? I tried with 'super' but my method wasn't successfull.
Thanks for your help
python inheritance
add a comment |
I'm a little confused about an inheritance problem in python.
I know the following example is "stupid" but I simplify my initial problem.
Imagine we have 3 classes
class testT(object):
def check(self, str):
return "t" in str
class testTE(testT):
def check(self, str):
return "e" in str
class testTES(testTE):
def check(self, str):
return "s" in str
And I would like an output like :
>>> print testTES().check("test")
>>> True (Because string "test" contains "s","e" and "t" characters)
>>> print testTES().check("dog")
>>> False
>>> print testTES().check("dogs")
>>> False (Contains a "s" but no "e" and no "t")
>>> print testTE().check("tuple")
>>> True (Contains "e" and "t")
How can I implement this behavior ? I tried with 'super' but my method wasn't successfull.
Thanks for your help
python inheritance
1
Just as a comment, note that this is not what is known as multiple inheritance. Here every class has a single parent so it is single inheritance (even if the inheritance hierarchy has more than one level). Multiple inheritance occurs when a class has more than one parent at the same time, leading to issues like the "diamond problem" and confusing priority rules in the case two inherited methods have the same signature (or just the same name, in Python).
– jdehesa
Nov 27 '18 at 15:51
add a comment |
I'm a little confused about an inheritance problem in python.
I know the following example is "stupid" but I simplify my initial problem.
Imagine we have 3 classes
class testT(object):
def check(self, str):
return "t" in str
class testTE(testT):
def check(self, str):
return "e" in str
class testTES(testTE):
def check(self, str):
return "s" in str
And I would like an output like :
>>> print testTES().check("test")
>>> True (Because string "test" contains "s","e" and "t" characters)
>>> print testTES().check("dog")
>>> False
>>> print testTES().check("dogs")
>>> False (Contains a "s" but no "e" and no "t")
>>> print testTE().check("tuple")
>>> True (Contains "e" and "t")
How can I implement this behavior ? I tried with 'super' but my method wasn't successfull.
Thanks for your help
python inheritance
I'm a little confused about an inheritance problem in python.
I know the following example is "stupid" but I simplify my initial problem.
Imagine we have 3 classes
class testT(object):
def check(self, str):
return "t" in str
class testTE(testT):
def check(self, str):
return "e" in str
class testTES(testTE):
def check(self, str):
return "s" in str
And I would like an output like :
>>> print testTES().check("test")
>>> True (Because string "test" contains "s","e" and "t" characters)
>>> print testTES().check("dog")
>>> False
>>> print testTES().check("dogs")
>>> False (Contains a "s" but no "e" and no "t")
>>> print testTE().check("tuple")
>>> True (Contains "e" and "t")
How can I implement this behavior ? I tried with 'super' but my method wasn't successfull.
Thanks for your help
python inheritance
python inheritance
asked Nov 27 '18 at 15:40
Renaud MichotteRenaud Michotte
748
748
1
Just as a comment, note that this is not what is known as multiple inheritance. Here every class has a single parent so it is single inheritance (even if the inheritance hierarchy has more than one level). Multiple inheritance occurs when a class has more than one parent at the same time, leading to issues like the "diamond problem" and confusing priority rules in the case two inherited methods have the same signature (or just the same name, in Python).
– jdehesa
Nov 27 '18 at 15:51
add a comment |
1
Just as a comment, note that this is not what is known as multiple inheritance. Here every class has a single parent so it is single inheritance (even if the inheritance hierarchy has more than one level). Multiple inheritance occurs when a class has more than one parent at the same time, leading to issues like the "diamond problem" and confusing priority rules in the case two inherited methods have the same signature (or just the same name, in Python).
– jdehesa
Nov 27 '18 at 15:51
1
1
Just as a comment, note that this is not what is known as multiple inheritance. Here every class has a single parent so it is single inheritance (even if the inheritance hierarchy has more than one level). Multiple inheritance occurs when a class has more than one parent at the same time, leading to issues like the "diamond problem" and confusing priority rules in the case two inherited methods have the same signature (or just the same name, in Python).
– jdehesa
Nov 27 '18 at 15:51
Just as a comment, note that this is not what is known as multiple inheritance. Here every class has a single parent so it is single inheritance (even if the inheritance hierarchy has more than one level). Multiple inheritance occurs when a class has more than one parent at the same time, leading to issues like the "diamond problem" and confusing priority rules in the case two inherited methods have the same signature (or just the same name, in Python).
– jdehesa
Nov 27 '18 at 15:51
add a comment |
1 Answer
1
active
oldest
votes
You just need to combine the check
calls in the subclasses with the output of super(...).check()
:
class testT(object):
def check(self, str):
return "t" in str
class testTE(testT):
def check(self, str):
return super(testTE, self).check(str) and "e" in str
class testTES(testTE):
def check(self, str):
return super(testTES, self).check(str) and "s" in str
print(testTES().check("test"))
# True
print(testTES().check("dog"))
# False
print(testTES().check("dogs"))
# False
print(testTE().check("tuple"))
# True
1
Note OP appears to be using Python 2, sosuper
andprint
would be slightly different in that case.
– jdehesa
Nov 27 '18 at 15:47
@jdehesa I fixed the calls tosuper
, in this caseprint(...)
works
– DeepSpace
Nov 27 '18 at 15:48
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53503161%2fpython-multiple-inheritance-problems%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
You just need to combine the check
calls in the subclasses with the output of super(...).check()
:
class testT(object):
def check(self, str):
return "t" in str
class testTE(testT):
def check(self, str):
return super(testTE, self).check(str) and "e" in str
class testTES(testTE):
def check(self, str):
return super(testTES, self).check(str) and "s" in str
print(testTES().check("test"))
# True
print(testTES().check("dog"))
# False
print(testTES().check("dogs"))
# False
print(testTE().check("tuple"))
# True
1
Note OP appears to be using Python 2, sosuper
andprint
would be slightly different in that case.
– jdehesa
Nov 27 '18 at 15:47
@jdehesa I fixed the calls tosuper
, in this caseprint(...)
works
– DeepSpace
Nov 27 '18 at 15:48
add a comment |
You just need to combine the check
calls in the subclasses with the output of super(...).check()
:
class testT(object):
def check(self, str):
return "t" in str
class testTE(testT):
def check(self, str):
return super(testTE, self).check(str) and "e" in str
class testTES(testTE):
def check(self, str):
return super(testTES, self).check(str) and "s" in str
print(testTES().check("test"))
# True
print(testTES().check("dog"))
# False
print(testTES().check("dogs"))
# False
print(testTE().check("tuple"))
# True
1
Note OP appears to be using Python 2, sosuper
andprint
would be slightly different in that case.
– jdehesa
Nov 27 '18 at 15:47
@jdehesa I fixed the calls tosuper
, in this caseprint(...)
works
– DeepSpace
Nov 27 '18 at 15:48
add a comment |
You just need to combine the check
calls in the subclasses with the output of super(...).check()
:
class testT(object):
def check(self, str):
return "t" in str
class testTE(testT):
def check(self, str):
return super(testTE, self).check(str) and "e" in str
class testTES(testTE):
def check(self, str):
return super(testTES, self).check(str) and "s" in str
print(testTES().check("test"))
# True
print(testTES().check("dog"))
# False
print(testTES().check("dogs"))
# False
print(testTE().check("tuple"))
# True
You just need to combine the check
calls in the subclasses with the output of super(...).check()
:
class testT(object):
def check(self, str):
return "t" in str
class testTE(testT):
def check(self, str):
return super(testTE, self).check(str) and "e" in str
class testTES(testTE):
def check(self, str):
return super(testTES, self).check(str) and "s" in str
print(testTES().check("test"))
# True
print(testTES().check("dog"))
# False
print(testTES().check("dogs"))
# False
print(testTE().check("tuple"))
# True
edited Nov 27 '18 at 15:47
answered Nov 27 '18 at 15:46
DeepSpaceDeepSpace
39.1k44473
39.1k44473
1
Note OP appears to be using Python 2, sosuper
andprint
would be slightly different in that case.
– jdehesa
Nov 27 '18 at 15:47
@jdehesa I fixed the calls tosuper
, in this caseprint(...)
works
– DeepSpace
Nov 27 '18 at 15:48
add a comment |
1
Note OP appears to be using Python 2, sosuper
andprint
would be slightly different in that case.
– jdehesa
Nov 27 '18 at 15:47
@jdehesa I fixed the calls tosuper
, in this caseprint(...)
works
– DeepSpace
Nov 27 '18 at 15:48
1
1
Note OP appears to be using Python 2, so
super
and print
would be slightly different in that case.– jdehesa
Nov 27 '18 at 15:47
Note OP appears to be using Python 2, so
super
and print
would be slightly different in that case.– jdehesa
Nov 27 '18 at 15:47
@jdehesa I fixed the calls to
super
, in this case print(...)
works– DeepSpace
Nov 27 '18 at 15:48
@jdehesa I fixed the calls to
super
, in this case print(...)
works– DeepSpace
Nov 27 '18 at 15:48
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53503161%2fpython-multiple-inheritance-problems%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Just as a comment, note that this is not what is known as multiple inheritance. Here every class has a single parent so it is single inheritance (even if the inheritance hierarchy has more than one level). Multiple inheritance occurs when a class has more than one parent at the same time, leading to issues like the "diamond problem" and confusing priority rules in the case two inherited methods have the same signature (or just the same name, in Python).
– jdehesa
Nov 27 '18 at 15:51