Multiple hierarchie and method overloading

P

Philippe Martin

Hi,




I have something like this:

Class A:
def A_Func(self, p_param):
.....
Class B:
def A_Func(self):
.....

Class C (A,B):
A.__init__(self)
B.__init__(self)

......

self.A_Func() #HERE I GET AN EXCEPTION "... takes at least 2 arguments (1
given).


I renamed A_Func(self) to fix that ... but is there a cleaner way around ?

Regards,

Philippe
 
B

Ben Cartwright

Philippe said:
I have something like this:

Class A:
def A_Func(self, p_param):
.....
Class B:
def A_Func(self):
.....

Class C (A,B):
A.__init__(self)
B.__init__(self)

.....

self.A_Func() #HERE I GET AN EXCEPTION "... takes at least 2 arguments (1
given).


I renamed A_Func(self) to fix that ... but is there a cleaner way around ?

When using multiple inheritence, the order of the base classes matters!
E.g.:

class A(object):
def f(self):
print 'in A.f()'
class B(object):
def f(self):
print 'in B.f()'
class X(A, B):
pass
class Y(B, A):
pass
in B.f()

If you want to call B.f() instead of A.f() for an X instance, you can
either rename B.f() like you've done, or do this:
in B.f()

--Ben
 
L

Lawrence D'Oliveiro

"Ben Cartwright said:
When using multiple inheritence, the order of the base classes matters!

When you have to start worrying about complications like this, isn't
that a sign that you're taking the whole OO thing a little too seriously?

After all, technology is supposed to _solve_ problems, not create them.
If the complications of OO are making you lose sight of your original
problem, then maybe you should put them aside.
 
B

bruno at modulix

Philippe said:
Hi,




I have something like this:

Class A:
def A_Func(self, p_param):
.....
Class B:
def A_Func(self):
.....

Class C (A,B):
A.__init__(self)

If that's really your code, you should have an exception right here.
Else, please post real code.

B.__init__(self)

.....

self.A_Func() #HERE I GET AN EXCEPTION "... takes at least 2 arguments (1
given).


I renamed A_Func(self) to fix that ... but is there a cleaner way around ?

Perhaps should you read these texts:

http://diveintopython.org/object_oriented_framework/defining_classes.html
http://www.freenetpages.co.uk/hp/alan.gauld/tutclass.htm
http://docs.python.org/tut/node11.html
 
B

bruno at modulix

Philippe said:
Hi,

I have something like this:

Class A:
def A_Func(self, p_param):
.....
Class B:
def A_Func(self):
.....

Class C (A,B):
A.__init__(self)
B.__init__(self)

.....

self.A_Func() #HERE I GET AN EXCEPTION "... takes at least 2 arguments (1
given).

Ho, yes, also: A.A_Func() really takes 2 arguments (self, and p_param).
When called from an instance of A, the first argument (ie: self) will be
automagically feed with the instance itself - but you still have to pass
the second one.
I renamed A_Func(self) to fix that ... but is there a cleaner way around ?

Yes : passing the second argument.
 
B

bruno at modulix

Lawrence said:
When you have to start worrying about complications like this, isn't
that a sign that you're taking the whole OO thing a little too seriously?

After all, technology is supposed to _solve_ problems, not create them.
If the complications of OO are making you lose sight of your original
problem, then maybe you should put them aside.


def fun(arg1, arg2):
pass

fun()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: fun() takes exactly 2 arguments (0 given)

If the complication of functions are making you loose sight of your
original problem, then maybe you should put them aside ?-)
 
P

Philippe Martin

Well, the whole point was to clean up my code:

Actually this is what I have:

Class A:
def A_Func(self, p_param):
.....
Class B:
def A_Func(self):
.....

Class C (A,B):
A.__init__(self)
B.__init__(self)

Class D (A,B):
A.__init__(self)
B.__init__(self)


Where A is a wxWidget class and B a "Common/utility" class which I wanted to
factorize (and yes, inheritance was not mandatory here, just simpler)

My common class does have an A_Func(self) while wxWidget an A_Func(self,
p_param) ==> I actually got the error calling A_Func(self) as it was
checked against A_Func(self, p_param).

Regards,

Philippe
 
P

Philippe Martin

Thanks,

I'll try that.

Philippe


Ben said:
When using multiple inheritence, the order of the base classes matters!
E.g.:

class A(object):
def f(self):
print 'in A.f()'
class B(object):
def f(self):
print 'in B.f()'
class X(A, B):
pass
class Y(B, A):
pass

in B.f()

If you want to call B.f() instead of A.f() for an X instance, you can
either rename B.f() like you've done, or do this:

in B.f()

--Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top