From: Back9 on 11 May 2010 15:06 Hi, Is this grammer working in Python? class test: self._value = 10 def func(self, self._value) When i try it, it complains about undefined self. i don't know why. TIA
From: Back9 on 11 May 2010 15:08 On May 11, 3:06 pm, Back9 <backgoo...(a)gmail.com> wrote: > Hi, > > Is this grammer working in Python? > > class test: > self._value = 10 > def func(self, self._value) > > When i try it, it complains about undefined self. > > i don't know why. > > TIA Sorry here is the what i meant class test: self._value = 10 def func(self, pos = self._value)
From: Chris Rebert on 11 May 2010 15:20 On Tue, May 11, 2010 at 12:08 PM, Back9 <backgoodoo(a)gmail.com> wrote: > On May 11, 3:06Â pm, Back9 <backgoo...(a)gmail.com> wrote: <snip> >> When i try it, it complains about undefined self. >> >> i don't know why. >> >> TIA > > Sorry > here is the what i meant > class test: > Â self._value = 10 > Â def func(self, pos = self._value) You're still defining the class, so how could there possibly be an instance of it to refer to as "self" yet (outside of a method body)? Also, just so you know, default argument values are only evaluated once, at the time the function/method is defined, so `pos = self._value` is never going to work. Do you mean for self._value to be a class variable (Java lingo: static variable), or an instance variable? Cheers, Chris -- http://blog.rebertia.com
From: Back9 on 11 May 2010 15:41 On May 11, 3:20 pm, Chris Rebert <c...(a)rebertia.com> wrote: > On Tue, May 11, 2010 at 12:08 PM, Back9 <backgoo...(a)gmail.com> wrote: > > On May 11, 3:06 pm, Back9 <backgoo...(a)gmail.com> wrote: > <snip> > >> When i try it, it complains about undefined self. > > >> i don't know why. > > >> TIA > > > Sorry > > here is the what i meant > > class test: > > self._value = 10 > > def func(self, pos = self._value) > > You're still defining the class, so how could there possibly be an > instance of it to refer to as "self" yet (outside of a method body)? > Also, just so you know, default argument values are only evaluated > once, at the time the function/method is defined, so `pos = > self._value` is never going to work. > > Do you mean for self._value to be a class variable (Java lingo: static > variable), or an instance variable? > > Cheers, > Chris > --http://blog.rebertia.com self._value will be instance variable
From: Chris Rebert on 11 May 2010 15:52
On Tue, May 11, 2010 at 12:41 PM, Back9 <backgoodoo(a)gmail.com> wrote: > On May 11, 3:20Â pm, Chris Rebert <c...(a)rebertia.com> wrote: >> On Tue, May 11, 2010 at 12:08 PM, Back9 <backgoo...(a)gmail.com> wrote: >> > On May 11, 3:06Â pm, Back9 <backgoo...(a)gmail.com> wrote: >> <snip> >> >> When i try it, it complains about undefined self. >> >> >> i don't know why. >> >> >> TIA >> >> > Sorry >> > here is the what i meant >> > class test: >> > Â self._value = 10 >> > Â def func(self, pos = self._value) >> >> You're still defining the class, so how could there possibly be an >> instance of it to refer to as "self" yet (outside of a method body)? >> Also, just so you know, default argument values are only evaluated >> once, at the time the function/method is defined, so `pos = >> self._value` is never going to work. >> >> Do you mean for self._value to be a class variable (Java lingo: static >> variable), or an instance variable? > > self._value will be instance variable class Test(object): def __init__(self): self._value = 10 def func(self, pos=None): if pos is None: pos = self._value #do whatever Using None like this is the idiomatic way to have non-constant or mutable default argument values in Python. I recommend you read the part of the Python tutorial on object-oriented programming: http://docs.python.org/tutorial/classes.html Cheers, Chris -- http://blog.rebertia.com |