Don't understand behavior; instance form a class in another class' instance

Rhodri James rhodri at
Fri Mar 26 02:10:36 CET 2010

On Fri, 26 Mar 2010 00:06:06 -0000, Martin P. Hellwig  
<martin.hellwig at> wrote:

> On 03/25/10 23:41, Christian Heimes wrote:
>> Martin P. Hellwig schrieb:
>>> What I don't understand why in the second test, the last boolean is  
>>> True
>>> instead of (what I expect) False.
>>> Could somebody enlighten me please as this has bitten me before and I  
>>> am
>>> confused by this behavior.
>> Hint: is not a reference to When you
>> alter you don't magically change,
>> too. Python doesn't have variables like C pointers. Python's copy by
>> object (or share by object) behavior can be understand as labels. The
>> label references the same object as
>> until you change where the label points to.
>> Christian
> Ah okay thanks for the explanation, Am I correct in thinking (please  
> correct me if I mangle up the terminology and/or totally are in the  
> wrong ballpark) that this is more or less because the label of the first  
> class is to an object (boolean with value False)
> and the label of the second class does not cascade to the first label  
> for looking something up but instead during assignment sees that it is a  
> label to an object instead of the object itself thus copies the label  
> content instead?

Pretty much.  In the sense that you're thinking of, every assignment works  
that way, even the initial "TEST1 = One()".  Assignment binds names to  
objects, though you have to be aware that names can be such exotic things  
as "t", "a[15]" or ""

> I probably expected classes namespaces to behave in about the same way  
> as lists and dictionaries do, don't know where I picked that up.

They do, in fact, which isn't terribly surprising considering that class  
namespaces are implemented with dictionaries.  The distinction you're  
missing is that lists and dictionaries are mutable, while booleans aren't;  
you can change the contents of a dictionary, but you can't change the  
'contents' of a boolean.

Rhodri James *-* Wildebeeste Herder to the Masses

More information about the Python-list mailing list