Re: python's OOP question
On Oct 16, 9:01 pm, Bruno Desthuilliers <o...@xiludom.g rowrote:
Bruno , your 2 great and clear samples revealed what method is in
python, which is also I really want to ask. thank you.
So I can reuse a method freely only if it's worth reusing.
For the word "inheritanc e", in some aspect, meanings reuse the super
class, with the condition: must reuse everything from super class.
It's lack of a option to select which methods are to be reused.
this is something should be improved for general OOP i think.
So to answer " What is your problem with having the other extra methods
too ?",
in real life, a class is not defined so well that any method is needed
by sub-class. so contains a method never to be used is meaningless and
i think something should forbidden.
also, any handy methods in a class can be grabbed out for our reuse. so
we can forgotting the bundering inheritance tree and order.
On Oct 16, 9:01 pm, Bruno Desthuilliers <o...@xiludom.g rowrote:
neoedmund wrote:
>
>
>
>
the main idea is to use the magic '__getattr__(se lf, name)' method.
>
Now back to your specific case : after a better reading of your original
question, straight composition/delegation wouldn't work here - at least
not without modifications to both C1 and C2 (sorry, should have read
better the first time).
>
Given the context (ie : "create a new type with methods from type X and
methods from type Y"), a very simple solution could be:
>
class C3(object):
m = C2.m.im_func
v = C1.v.im_func
>
FWIW, if you have full control over C1, C2 and C3, you could also just
'externalize' the functions definitions:
>
def v1(self, o):
return "expected "+o
>
def v2(self, o):
return "unexpected "+o
>
def m2(self):
""" requires that 'self' has a v(self, somestring) method """
print self.v("aaa")
>
class C1(object):
v = v1
>
class C2(object):
v = v2
m = m2
>
class C3(object):
v = v1
m = m2
>
The problem (with the whole approach, whatever the choosen technical
solution) is that if one of theses methods depends on another one (or on
any other attribute) that is not defined in your new class, you're in
trouble. This is not such a big deal in the above example, but might
become much more brittle in real life.
>
Now we can look at the problem from a different perspective. You wrote:
"""
but "inheritanc e" means you must inherite all methods from super type.
now i just need "some" methods from one type and "some" methods from
other types, to build the new type.
"""
>
What is your problem with having the other extra methods too ?
>
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'on...@xiludom. gro'.split('@')])"
Bruno Desthuilliers wrote:
neoedmund wrote:
(*PLEASE* stop top-posting - corrected)
>Ben Finney wrote:
>>[Please don't top-post above the text to which you're replying.]
(*PLEASE* stop top-posting - corrected)
>Ben Finney wrote:
>>[Please don't top-post above the text to which you're replying.]
>>"neoedmund" <neoedm...@gmai l.comwrites:
>>>I'm trying to achieve a higher level of "reusabilit y". Maybe it
>>>cannot be done in python? Can anybody help me?
>>What, specifically, are you trying to achieve? What problem needs
>>solving?
>python use multiple inheritance.
>but "inheritanc e" means you must inherite all methods from super type.
>now i just need "some" methods from one type and "some" methods from
>other types, to build the new type.
>Do you think this way is more flexible than tranditional inheritance?
>>>cannot be done in python? Can anybody help me?
>>What, specifically, are you trying to achieve? What problem needs
>>solving?
>python use multiple inheritance.
>but "inheritanc e" means you must inherite all methods from super type.
>now i just need "some" methods from one type and "some" methods from
>other types, to build the new type.
>Do you think this way is more flexible than tranditional inheritance?
While dynamically adding attributes (and methods - which are attributes
too) is not a problem, I'd second Gregor's anwser : it might be better
to use composition/delegation here.
too) is not a problem, I'd second Gregor's anwser : it might be better
to use composition/delegation here.
Could you show some code to help me know how composition/delegation can
be done here? Thanks.About composition/delegation, there's no "one-size-fits-all" answer, but
be done here? Thanks.About composition/delegation, there's no "one-size-fits-all" answer, but
>
Now back to your specific case : after a better reading of your original
question, straight composition/delegation wouldn't work here - at least
not without modifications to both C1 and C2 (sorry, should have read
better the first time).
>
Given the context (ie : "create a new type with methods from type X and
methods from type Y"), a very simple solution could be:
>
class C3(object):
m = C2.m.im_func
v = C1.v.im_func
>
FWIW, if you have full control over C1, C2 and C3, you could also just
'externalize' the functions definitions:
>
def v1(self, o):
return "expected "+o
>
def v2(self, o):
return "unexpected "+o
>
def m2(self):
""" requires that 'self' has a v(self, somestring) method """
print self.v("aaa")
>
class C1(object):
v = v1
>
class C2(object):
v = v2
m = m2
>
class C3(object):
v = v1
m = m2
>
The problem (with the whole approach, whatever the choosen technical
solution) is that if one of theses methods depends on another one (or on
any other attribute) that is not defined in your new class, you're in
trouble. This is not such a big deal in the above example, but might
become much more brittle in real life.
>
Now we can look at the problem from a different perspective. You wrote:
"""
but "inheritanc e" means you must inherite all methods from super type.
now i just need "some" methods from one type and "some" methods from
other types, to build the new type.
"""
>
What is your problem with having the other extra methods too ?
>
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'on...@xiludom. gro'.split('@')])"
python, which is also I really want to ask. thank you.
So I can reuse a method freely only if it's worth reusing.
For the word "inheritanc e", in some aspect, meanings reuse the super
class, with the condition: must reuse everything from super class.
It's lack of a option to select which methods are to be reused.
this is something should be improved for general OOP i think.
So to answer " What is your problem with having the other extra methods
too ?",
in real life, a class is not defined so well that any method is needed
by sub-class. so contains a method never to be used is meaningless and
i think something should forbidden.
also, any handy methods in a class can be grabbed out for our reuse. so
we can forgotting the bundering inheritance tree and order.
Comment