Class attribute or instance attribute?

Paul Watson pwatson at redlinec.com
Mon Apr 28 14:47:28 EDT 2003


When is an attribute a class attribute or an instance attribute?

In the following code, is the attribute (variable/member) a class attribute
or an instance attribute?  I thought that it was an instance attribute and
that the class attribute was created in __init__().

My biggest question is why self.v has a value of "Consequences" in the
_init__() of f2?  Is self.v not an instance variable?

pwatson [ /home/pwatson/src/python/c ] 270
$ cat d.py
#! /usr/bin/env python

class whatisit:
  v = "Ambiguous"

  def __init__(self):
    print '=== __init__', self.v, whatisit.v
    self.v = "Truth"
    whatisit.v = "Consequences"

  def showem(self):
    print '=== showem  ', self.v, whatisit.v

if (__name__ == "__main__"):
  f1 = whatisit()
  f1.showem()
  f2 = whatisit()
  f2.showem()
pwatson [ /home/pwatson/src/python/c ] 271
$ ./d.py
=== __init__ Ambiguous Ambiguous
=== showem   Truth Consequences
=== __init__ Consequences Consequences
=== showem   Truth Consequences






More information about the Python-list mailing list