Overloading methods?

Jerome Chan eviltofu at rocketmail.com
Fri Apr 21 17:27:41 EDT 2000


I have the following:

class mc:
    def __init__(self,a):
        self.a = a
    def dosomething(self,a):
        self.a = self.a + a

class yc(mc):
    def __init__(self,a,b):
        mc.__init__(self,a)
        self.b = b
    def dosomething(self,a,b):
        mc.dosomething(self,a)
        self.b = self.b + b

x = mc(1)
y = yc(1,2)

x.dosomething(1)
y.dosomething(1,2)

y = yc(1) <--- Got expected 3 but got 2 error
y.dosomething(1) <--- Got exptected 3 but got 2 error

Does this mean that method overloading is not allowed?

I tried the following workaround, by dispatching the call myself:

class mc:
    def __init__(self,a1):
        self.a = a1
    def dosomething(self,a1):
        self.a = self.a + a1

class yc(mc):
    def __init__(self,a1=None,b1=None):
        if a1:
            mc.__init__(self,a1)
        if b1:
            self.b = b1
    def dosomething(self,a1=None,b1=None):
        if a1:
            mc.dosomething(self,a1)
        if b1:
            self.b = self.b + b1

x = mc(1)
y = yc(1)

x.dosomething(1)
y.dosomething(b=1,a=2) <-- got attribute error: b

So it seems that if if you can't use b until it has been instantiated 
previously.

By the way, being able to call methods with keyword arguments in any 
order is one of the neatess things I like about python.



More information about the Python-list mailing list