What a curious assignment.

Mike Meyer mwm at mired.org
Wed Nov 23 00:02:44 EST 2005


"Neil.Jin at gmail.com" <Neil.Jin at gmail.com> writes:

> [test 1]
>>>> class A:
> ...    i = 1
> ...
>>>> a = A()
>>>> A.i
> 1
>>>> a.i
> 1
>>>> A.i = 2
>>>> A.i
> 2
>>>> a.i
> 2
>>>>
>
> [test2]
>>>> class A:
> ...    i = 1
> ...
>>>> a = A()
>>>> A.i
> 1
>>>> a.i
> 1
>>>> a.i = 2
>>>> A.i
> 1
>>>> a.i
> 2
>>>>
>
> Is there somthing wrong????

No. Reading a.i looks up i by checking the instance (a), then the
class (A), so a.i and A.i are the same thing. So changing A.i changes
the value seen by a.i. Binding a.i binds i to a, not A, so after the
binding, a.i and A.i are different things.

         <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list