This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: dictionary referencing error
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ckbcouture, rhettinger
Priority: normal Keywords:

Created on 2004-08-19 14:57 by ckbcouture, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg22136 - (view) Author: AMIT Consulting LLC (ckbcouture) Date: 2004-08-19 14:57
When I write a class as such:


class MyObject:

   myDict = {}

   ...


Each new instance refers to the SAME dictionary, not a 
newly minted one.  However, this does not hold true for 
other data types.

This has forced me to place dictionary declarations in 
the __init__ method to ensure uniqueness.

Unless this is intended for some obscure reason, it 
should be fixed.

Thanks,
Cameron


msg22137 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-08-19 16:00
Logged In: YES 
user_id=80475

That is the intended behavior.  Attribute definitions inside
the class definition but outside the method definitions are
essentially class variables.  They are useful because each
instance does not need to have its own copy.  For cases
where an instance needs its own copy, your solution using
__init__ was the right thing to do.

In time, this will be obvious and all will feel as right as
rain.
msg22138 - (view) Author: AMIT Consulting LLC (ckbcouture) Date: 2004-08-19 16:09
Logged In: YES 
user_id=1096248

The problem is that this only seems to work for dictionaries, 
tuples and lists.  it DOES NOT work for numbers and strings.

Try the following:

class Test:

        mint = 1
        mchar = "A"
        mdict = {}
        mlist = []
        mtup = ()

        def __init__( self ):
                self.mint += 1
                self.mchar += self.mchar
                self.mdict[ self.mchar ] = self.mint
                self.mlist.append( self.mdict )
                self.mtup += ( self.mlist, )
                print self.mtup


for i in range(10):
        Test()

For me it outputs:

([{'AA': 2}],)
([{'AA': 2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 
2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 
2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 
2}, {'AA': 2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 
2}, {'AA': 2}, {'AA': 2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 
2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}],)

Which means that it is working as you say for some, but not 
all data types.  Is THAT intended?

Thanks,
Cameron
msg22139 - (view) Author: AMIT Consulting LLC (ckbcouture) Date: 2004-08-19 16:20
Logged In: YES 
user_id=1096248

The problem is that this only seems to work for dictionaries, 
tuples and lists.  it DOES NOT work for numbers and strings.

Try the following:

class Test:

        mint = 1
        mchar = "A"
        mdict = {}
        mlist = []
        mtup = ()

        def __init__( self ):
                self.mint += 1
                self.mchar += self.mchar
                self.mdict[ self.mchar ] = self.mint
                self.mlist.append( self.mdict )
                self.mtup += ( self.mlist, )
                print self.mtup


for i in range(10):
        Test()

For me it outputs:

([{'AA': 2}],)
([{'AA': 2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 
2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 
2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 
2}, {'AA': 2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 
2}, {'AA': 2}, {'AA': 2}, {'AA': 2}],)
([{'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 
2}, {'AA': 2}, {'AA': 2}, {'AA': 2}, {'AA': 2}],)

Which means that it is working as you say for some, but not 
all data types.  Is THAT intended?

Thanks,
Cameron
msg22140 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-08-19 17:14
Logged In: YES 
user_id=80475

This is also the way it is supposed to be.  At issue is
whether the object is mutable or not.  If mutable, something
like .append() alters the object inplace (at the class
level).  If immutable, something like a = a + b reads a from
the class level, adds b creating a newobject, and stores the
new object, a, back at the instance level.

Please work through the tutorial or make a newsgroup posting
requesting better explanations.  This isn't a bug, it is
just something intrinsic to Python and is a minor step that
everyone has to go through on the path to mastering the
language.
History
Date User Action Args
2022-04-11 14:56:06adminsetgithub: 40787
2004-08-19 14:57:23ckbcouturecreate