[ python-Bugs-1012249 ] dictionary referencing error

SourceForge.net noreply at sourceforge.net
Thu Aug 19 19:14:08 CEST 2004


Bugs item #1012249, was opened at 2004-08-19 09:57
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1012249&group_id=5470

Category: Parser/Compiler
Group: Python 2.3
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: AMIT Consulting LLC (ckbcouture)
Assigned to: Nobody/Anonymous (nobody)
Summary: dictionary referencing error

Initial Comment:
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




----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2004-08-19 12:14

Message:
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.

----------------------------------------------------------------------

Comment By: AMIT Consulting LLC (ckbcouture)
Date: 2004-08-19 11:20

Message:
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

----------------------------------------------------------------------

Comment By: AMIT Consulting LLC (ckbcouture)
Date: 2004-08-19 11:09

Message:
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

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2004-08-19 11:00

Message:
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.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1012249&group_id=5470


More information about the Python-bugs-list mailing list