Class attribute or instance attribute? 
Author Message
 Class attribute or instance attribute?

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



Sat, 15 Oct 2005 02:47:28 GMT  
 Class attribute or instance attribute?

Quote:

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

I give you a subtle example:

class C(object):
      x=1 # class attribute
      def __init__(self):
          self.x=self.x # the class attribute becomes an instance attribute

c=C()

C.x=2

print c.x,C.x

This code prints 1,2. You see that the line self.x=self.x is far
from being trivial: Python looks first for the self on the right,
looking for the instance attribute self.x: since it does not find anything,
it looks for the class attribute; then the class attribute
is assigned to the instance attribute on the left hand side.

If you forget the apparently useless self.x=self.x line, you get a
different result:

class C(object):
      x=1 # class attribute
      def __init__(self):
          pass

c=C()

C.x=2

print c.x,C.x

This prints 2,2, since the class attribute has been changed to 2
and c has no instance attribute.

Hoping-having-not-increased-your-confusion-ly,

                    Michele



Sat, 15 Oct 2005 05:29:33 GMT  
 Class attribute or instance attribute?

Quote:

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

When an attribute lookup fails in an instance, Python searches the
namespace of the parent class and all of the parent's base classes (but
*not* the namespaces of the metaclasses).
--

"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles:  boring syntax, unsurprising semantics,
few automatic coercions, etc etc.  But that's one of the things I like
about it."  --Tim Peters on Python, 16 Sep 93



Sat, 15 Oct 2005 04:39:00 GMT  
 Class attribute or instance attribute?
Thank you for your reply.  I have finally gotten it that the attribute 'x'
is already a class attribute and not an instance attribute.  It appears that
there can only be an instance attribute when someone says self.x or
instance.x.  I used dir(C) and dir(self) to see what attributes existed.  Is
there any other way to declare instance variables?

Thanks for everyone's help, including Mike.



Quote:
> > When is an attribute a class attribute or an instance attribute?

> I give you a subtle example:

> class C(object):
>       x=1 # class attribute
>       def __init__(self):
>           self.x=self.x # the class attribute becomes an instance
attribute

> c=C()

> C.x=2

> print c.x,C.x

> This code prints 1,2. You see that the line self.x=self.x is far
> from being trivial: Python looks first for the self on the right,
> looking for the instance attribute self.x: since it does not find
anything,
> it looks for the class attribute; then the class attribute
> is assigned to the instance attribute on the left hand side.

> If you forget the apparently useless self.x=self.x line, you get a
> different result:

> class C(object):
>       x=1 # class attribute
>       def __init__(self):
>           pass

> c=C()

> C.x=2

> print c.x,C.x

> This prints 2,2, since the class attribute has been changed to 2
> and c has no instance attribute.

> Hoping-having-not-increased-your-confusion-ly,

>                     Michele



Sat, 15 Oct 2005 11:36:44 GMT  
 Class attribute or instance attribute?
Quote:


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

> I give you a subtle example:

On the other hand here is a totally obvious example illustrating the same ;-)

class Foo (object) :
    foo = 'spam'
    def __init__ (self) :
        print Foo.foo, self.foo
        self.foo = 'ham'
        print Foo.foo, self.foo

Quote:
>>> f = Foo()

spam spam
spam ham
Quote:
>>> print f.foo, Foo.foo

ham spam


Sat, 15 Oct 2005 12:50:41 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Using cPickle, pickle or marshal to preserve attribute values of a class instance

2. Visibility of CLASS and INSTANCE attributes

3. Attributes / User Defined Attributes

4. TIP #222: Add [wm attributes -alpha] Attribute on Windows

5. Virtual access of class attribute from within the class

6. Types (was: Attribute vs Instance Variable)

7. del(instance.attr) -- no attribute

8. Viewing instance attributes in IDLE's debugger

9. unintentionaly shared attributes in two different instances

10. storing instance attributes to a database

11. Querying an instance's attributes

12. bug when attribute name same as instance?

 

 
Powered by phpBB® Forum Software