Python Polymorphism

Dan Sommers me at privacy.net
Thu May 12 16:16:48 EDT 2005


On Thu, 12 May 2005 13:00:45 -0700 (PDT),
Carlos Moreira <duduca007 at yahoo.com> wrote:

> Look, I don't want to use things like:

> ....
>       def myMethod(self, myValue=None):
>            if(not myValue):
>                 self.myAttribute += 1
>            else:
>                 self.myAttribute += myValue
> ....

How about:

    def myMethod( self, myValue = 1 ):
        self.myAttribute += myValue

Callers of myMethod look exactly like they would look if you had written
two methods:

    a.myMethod( ) # add 1 to a.myAttribute
    a.myMethod( 22 ) # add 22 to a.myAttribute

> I want to use the power of polymorphism to modelate the problem.

Polymorphism in Python is a solution looking for a problem.

Regards,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>



More information about the Python-list mailing list