Multiple hierarchie and method overloading

Discussion in 'Python' started by Philippe Martin, Apr 25, 2006.

  1. 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
    Philippe Martin, Apr 25, 2006
    #1
    1. Advertising

  2. 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
    Ben Cartwright, Apr 25, 2006
    #2
    1. Advertising

  3. Re: Multiple hierarchies and method overloading

    In article <>,
    "Ben Cartwright" <> 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.
    Lawrence D'Oliveiro, Apr 25, 2006
    #3
  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)


    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


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    bruno at modulix, Apr 25, 2006
    #4
  5. 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 ''.split('@')])"
    bruno at modulix, Apr 25, 2006
    #5
  6. Re: Multiple hierarchies and method overloading

    Lawrence D'Oliveiro wrote:
    > In article <>,
    > "Ben Cartwright" <> 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 ''.split('@')])"
    bruno at modulix, Apr 25, 2006
    #6
  7. Re: Multiple hierarchies and method overloading

    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 <>,
    > "Ben Cartwright" <> 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.
    Philippe Martin, Apr 25, 2006
    #7
  8. 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
    Philippe Martin, Apr 25, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piet
    Replies:
    2
    Views:
    353
    Tom B.
    Sep 10, 2004
  2. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,396
    Fredrik Lundh
    Sep 30, 2005
  3. Fredrik Lundh
    Replies:
    0
    Views:
    442
    Fredrik Lundh
    Sep 30, 2005
  4. Steve Holden
    Replies:
    0
    Views:
    423
    Steve Holden
    Sep 30, 2005
  5. Iyer, Prasad C
    Replies:
    4
    Views:
    571
    John J. Lee
    Sep 30, 2005
Loading...

Share This Page