[C++-sig] Multiple python interpreter contexts

Stefan Seefeld seefeld at sympatico.ca
Wed Feb 23 00:13:49 CET 2005


Ralf W. Grosse-Kunstleve wrote:

> http://www.boost.org/libs/python/doc/tutorial/doc/html/python/object.html
> 
> Look for "Beware" and "astute".

"

Beware the common pitfall of forgetting that the constructors of most of Python's mutable types make 
copies, just as in Python.

Python:

 >>> d = dict(x.__dict__)     # copies x.__dict__
 >>> d['whatever']            # modifies the copy

"

This is confusing. May be I'm reading the above not correctly, but in my interpretation
'making a copy' means making a deep copy, and 'modifies the copy' implies the original
is unchanged. However:

 >>> class A:
...     def foo(self): return 42
...
 >>> d = A.__dict__
 >>> print id(d), id(A.__dict__)
-1208268764 -1208268764
 >>> d['foo'] = 'bar'
 >>> print A.__dict__
{'__module__': '__main__', 'foo': 'bar', '__doc__': None}

confirms our previous finding, i.e. 'd = A.__dict__' does *not* create a copy of
A's dictionary, but instead creates a new reference ('binding') to it.

Can you please clarify the meaning of 'copy' in the documentation ?

Thanks a lot,
		Stefan



More information about the Cplusplus-sig mailing list