default argument in method

ernest nfdisco at gmail.com
Sun Dec 12 06:35:22 EST 2010


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



More information about the Python-list mailing list