470,618 Members | 1,786 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,618 developers. It's quick & easy.

Multiple hierarchie and method overloading

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

Apr 25 '06 #1
7 896
Philippe Martin wrote:
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
x = X()
x.f() in A.f() y = Y()
y.f() 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:
B.f(x)

in B.f()

--Ben

Apr 25 '06 #2
In article <11**********************@v46g2000cwv.googlegroups .com>,
"Ben Cartwright" <be****@gmail.com> wrote:
Philippe Martin wrote:
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!


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.
Apr 25 '06 #3
Philippe Martin wrote:
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_ori...g_classes.html
http://www.freenetpages.co.uk/hp/ala...d/tutclass.htm
http://docs.python.org/tut/node11.html
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 25 '06 #4
Philippe Martin wrote:
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.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 25 '06 #5
Lawrence D'Oliveiro wrote:
In article <11**********************@v46g2000cwv.googlegroups .com>,
"Ben Cartwright" <be****@gmail.com> wrote:

Philippe Martin wrote:

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!

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 ?-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 25 '06 #6
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



Lawrence D'Oliveiro wrote:
In article <11**********************@v46g2000cwv.googlegroups .com>,
"Ben Cartwright" <be****@gmail.com> wrote:
Philippe Martin wrote:
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!


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.


Apr 25 '06 #7
Thanks,

I'll try that.

Philippe
Ben Cartwright wrote:
Philippe Martin wrote:
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
>>> x = X()
>>> x.f() in A.f() >>> y = Y()
>>> y.f() 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:
>>> B.f(x)

in B.f()

--Ben


Apr 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by alex | last post: by
4 posts views Thread by John Smith | last post: by
18 posts views Thread by Daniel Gustafsson | last post: by
11 posts views Thread by placid | last post: by
5 posts views Thread by Andreas Schmitt | last post: by
10 posts views Thread by Matthew | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.