-1

To quote diveintopython,

"You already know about data attributes, which are variables owned by a specific instance of a class. Python also supports class attributes, which are variables owned by the class itself."

In what sense are class attributes owned by a class? If you change the value of a class attribute in a specific instance, that change is only reflected in that instance (and not in other instances of the class).

From my vantage point this makes class attributes fundamentally the same as data (i.e. instance) attributes (notwithstanding the syntactic differences).

In C++ change the value of a "class variable", and that change is reflected in all instances.

What is the difference between the two?

Blender
  • 289,723
  • 53
  • 439
  • 496
Mark
  • 1,214
  • 10
  • 24
  • Blender - looks like you had time to completely rewrite my post to take the edge off but still couldn't answer it. Thanks? (FYI- The way you've worded the title, it will be marked a duplicate, although the question has NOT been answered anywhere at SO to my knowledge.) – Mark Dec 20 '11 at 07:40
  • What do you mean by "edge"? I cleaned up your question, as you were just starting a casual discussion and not asking anything concrete. If you don't like my edit, roll back to your original state. This has been answered [quite a few times before](http://stackoverflow.com/questions/207000/python-difference-between-class-and-instance-attributes). – Blender Dec 20 '11 at 07:43
  • Blender's edit is a big improvement. – wim Dec 20 '11 at 07:43
  • Blender, for starters you took out the part where I say "don't bother replying if you've only programmed in Python." I wanted confirmation from say, a C++ programmer, to the effect, "Yeah this behavior was quite unexpected for me as well. Can't really explain it either." There is a reason this question is repeatedly brought up. – Mark Dec 20 '11 at 08:10

2 Answers2

1

I think that this example will explain the meaning to you.

class A(object):
    bar = 1

a = A()
b = A()
b.bar = 2
print a.bar  # outputs 1
A.bar = 3
print a.bar  # outputs 3
print b.bar  # outputs 2

In this case b.bar will be owned by instance after b.bar = 2 but a.bar will still be owned by class. That is why it will be changed on instance after changing it on class and b.bar will not.

lig
  • 3,567
  • 1
  • 24
  • 36
  • The OP asked why there is a *distinction* between class attributes and data attributes, as they seem to perform the same task. – Blender Dec 20 '11 at 07:45
  • OK...So it is a literal class variable, but only for instances that have not modified it themselves. If a class variable is modified for a specific instance it immediately starts behaving like an instance attribute. Don't understand the purpose of this. – Mark Dec 20 '11 at 07:46
  • This is not "static" at all. Traditionally a class variable should be static across all instances, so change it anywhere and that change is reflected in all instances. This behavior of python class attributes is subtle and bizarre (and seemingly pointless). – Mark Dec 20 '11 at 07:51
1

This question is a duplicate of this one:

>>> class B(object):
...     foo = 1
... 
>>> b = B()
>>> b.__dict__
{}
>>> b.foo = 2
>>> b.__dict__
{'foo': 2}

When you assign a value to b, you add an instance variable; you're not modifying the class attribute.

Community
  • 1
  • 1
Bouke
  • 11,768
  • 7
  • 68
  • 102
  • Its not a duplicate, just because it mentions "class attribute" Anyway that guys half page answer is verbose but vague and still doesn't explain the odd behavior of python class attributes. Anyone can cut and paste from a python manual too. – Mark Dec 20 '11 at 08:03