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?
"Paul Watson" <pwat...@redlinec.com> wrote in message <news:vaqtlsqbi52j93@corp.supernews.com>... > 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.
>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). -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/
"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
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.
"Michele Simionato" <m...@pitt.edu> wrote in message
> > 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.