using attributes as defaults

Emile van Sebille emile at fenx.com
Fri Feb 4 22:20:06 CET 2011


On 2/4/2011 1:08 PM Wanderer said...
> I want to give the option of changing attributes in a method or using
> the current values of the attributes as the default.
>
> class MyClass():
>       """  my Class
>       """
>       def __init__(self):
>            """ initialize
>            """
>            self.a = 3
>            self.b = 4
>
>       def MyMethod(self, a = self.a, b = self.b)
>            """ My Method
>            """
>            self.a = a
>            self.b = b
>            DoSomething(a, b)

The typical approach would be to use None:

     def myMethod(self,a=None,b=None):
         if a is None: a=self.a
         ...

Emile


>
> The above doesn't work. Is there a way to make it work?
>
> Thanks
>





More information about the Python-list mailing list