On Sun, Dec 12, 2010 at 5:05 PM, ernest <nfdi...@gmail.com> 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. >
You can defined foo outside the __init__ and can access it inside methods using self.foo class C(object): foo=5 def __init__(self): print self.foo def m(self,val=foo): return val class attributes can be accessed with out self before them in method signatures. However, becareful if you change the value of foo inside any method, the method signature will still hold on to old value. -- Thanks & Regards, Godson Gera Python Consultant India <http://godson.in>
-- http://mail.python.org/mailman/listinfo/python-list