Python : Multiple inheritance problems












0















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










share|improve this question


















  • 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


















0















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










share|improve this question


















  • 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
















0












0








0








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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














1 Answer
1






active

oldest

votes


















4














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





share|improve this answer





















  • 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











  • @jdehesa I fixed the calls to super, in this case print(...) works

    – DeepSpace
    Nov 27 '18 at 15:48











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%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









4














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





share|improve this answer





















  • 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











  • @jdehesa I fixed the calls to super, in this case print(...) works

    – DeepSpace
    Nov 27 '18 at 15:48
















4














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





share|improve this answer





















  • 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











  • @jdehesa I fixed the calls to super, in this case print(...) works

    – DeepSpace
    Nov 27 '18 at 15:48














4












4








4







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








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, 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














  • 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











  • @jdehesa I fixed the calls to super, in this case print(...) 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




















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%2f53503161%2fpython-multiple-inheritance-problems%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







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks