If you declare aaa in the class body it will be a class varaible instead of an instance variable which means that it's shared by all instances of this class. If you want each myclass to have it's own aaa you must declare a constructor that initializes this special instance like this: class myclass: def __init__ (self, dict = None): # by looking at your code I presume that you want to initialize your class with a dictionary if dict is None: dict = {} self.aaa=dict def add(self,u): (self.aaa)[u]=1 def show(self): print self.aaa HTH, Nils "Jacek Pliszka" <pliszka at fuw.edu.pl> schrieb im Newsbeitrag news:Pine.LNX.4.30.0104232122310.23315-100000 at ift4.fuw.edu.pl... > Hi! > > I just started to learn Python and I got stuck with the following > problem: > > class myclass: > aaa={} > def add(self,u): > (self.aaa)[u]=1 > def show(self): > print self.aaa > > ula=myclass({}) > > ula.add(13) > > ola=myclass({}) > > ula.add(11) > > ola.add(11) > > ula.show() > > ola.show() > > then I get the result that aaa in both ula and ola > is identical!!! I thought that aaa will be different for > ula and ola!! > > What am I doing wrong? > > Thanks in advance for any help, > > Jacek > >
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4