On Mar 6, 5:04 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Thu, 06 Mar 2008 22:48:42 -0200, Krishna <[EMAIL PROTECTED]> > escribi�: > > > > >>>> class Test(object): > > ... def __init__(self): > > ... self.a= 2 > > ... def func(self, k = self.a): > > ... print k > > ... > > Traceback (most recent call last): > > File "<stdin>", line 1, in ? > > File "<stdin>", line 4, in Test > > NameError: name 'self' is not defined > > > In the 'definition of the class', what would the first argument 'self' > > in the methods evaluate to; when we have an object defined, it is > > bound to the object reference, but what happens while the class > > definition is executed, which I believe happens when the module > > containing the class definition is imported > > Function default arguments are evaluated when the function is defined > (when the class is defined, in this case) so "self" itself has not a > value. Try this instead: > > def func(self, k=None): > if k is None: > k = self.a > print k > > If None is an allowed argument, use a special marker instead: > > _marker=object() > ... > > def func(self, k=_marker): > if k is _marker: > k = self.a > ... > > -- > Gabriel Genellina
Thanks for the reply. I am currently using the approach suggested by you. But, I am more interested in knowing about the first argument ('self'), what does it hold to allow the evaluation of the method, take the example you gave, 'self.a' as Rvalue inside the method, how and why is this allowed, when the same 'self.a' is not allowed as the default argument, considering the fact that I have already specified 'self' as first argument, only after whose evaluation, I believe would the next statement (k = self.a, in def func(self, k = self.a) ) gets evaluated Thanks, Krishna -- http://mail.python.org/mailman/listinfo/python-list