[ python-Bugs-1012249 ] dictionary referencing error

SourceForge.net noreply at sourceforge.net
Thu Aug 19 18:09:36 CEST 2004


Bugs item #1012249, was opened at 2004-08-19 10:57
Message generated for change (Comment added) made by ckbcouture
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: AMIT Consulting LLC (ckbcouture)
Date: 2004-08-19 12: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 12: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