Class initialization

Tim Harig usernet at ilthio.net
Sun Aug 8 11:01:53 EDT 2010


On 2010-08-08, Costin Gament <costin.gament at gmail.com> wrote:
> Thank you for your answer, but it seems I didn't make myself clear.
> Take the code:
> class foo:
>   a = 0
>   b = 0
> c1 = foo()
> c1.a = 5
> c2 = foo()
> print c2.a
> 5
>
> Somehow, when I try to acces the 'a' variable in c2 it has the same
> value as the 'a' variable in c1. Am I missing something?

Others have told you that at a and b belong to the class object rather then
to the instance objects.  Perhaps this will demonstrate the difference:

>>> class foo():
...     def __init__(self):
...             self.a = 0
...             self.b = 0
...
>>> c1 = foo()
>>> c1.a = 5
>>> c2 = foo()
>>> print c2.a
0
>>>



More information about the Python-list mailing list