Dict in Class Problem

Terry Reedy tjreedy at udel.edu
Tue Oct 8 09:05:40 EDT 2002


"Sebastian Haas" <haas at ems-wuensche.com> wrote in message
news:3DA2F0C2.8060801 at ems-wuensche.com...
> Hello NG,
>
> i have a little problem in Python.
>
> code:
>
> #! /usr/bin/python
>
> class CTest:
> dicton = {}
>
> first = CTest()
> secon = CTest()
>
> first.dicton['test'] = "RE"
> secon.dicton['test'] = "BE"
>
> print first.dicton['test']
>
> Why is the output "BE", why not "RE".
>
> With
> first = CTest()
> secon = CTest()
> i get 2 new object, 2 objects that differs, or not??
>
> The Keys are the same, okay, but i have 2 different objects (with 2
> different dictionarys).

No, you have one dictionary, not two (try 'first.dicton ==
secon.dicton').  Read more about the difference between class and
instance attribute and about __init__() functions.





More information about the Python-list mailing list