Newbie question on self in classes

Klaus Oberpichler klaus.oberpichler at freenet.de
Sat Jan 10 16:21:22 EST 2004


Hello Python community,

my simple question is: When is the self reference to a class valid?
During playing around with python I got the following error message:

>>> class Test:
...  value = 1000
...  self.value = 2
...
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "<interactive input>", line 3, in Test
NameError: name 'self' is not defined

When I do it a different way no error occurs:

>>> class Test:
...  value = 1000
...  def f(self):
...   self.value = 2
...
>>>

When is the reference to its own class is generated and valid? What is the
reason bihind that behaviour?

Best regards
Klaus





More information about the Python-list mailing list