default argument in method
Jean-Michel Pichavant
jeanmichel at sequans.com
Mon Dec 13 05:51:38 EST 2010
ernest wrote:
> Hi,
>
> I'd like to have a reference to an instance attribute as
> default argument in a method. It doesn't work because
> "self" is not defined at the time the method signature is
> evaluated. For example:
>
> class C(object):
> def __init__(self):
> self.foo = 5
> def m(self, val=self.foo):
> return val
>
> Raises NameError because 'self' is not defined.
> The obvious solution is put val=None in the signature
> and set val to the appropriate value inside the method
> (if val is None: ...), but I wonder if there's another way.
>
> Cheers,
> Ernest
>
your 'val=None' is jus fine.
JM
More information about the Python-list
mailing list