Changing a COPY of metadata modifies ZClass instance?
I think I'm going slightly mad :-) A Python Script that is supposed to modify a COPY of metadata in practice changes metadata itself, and ZClass instance :-( for i in context.Catalog_Org(userid=uid): l=i.userid print "title: ",i.title l.remove(uid) When this script runs from ZMI, it changes metadata in Catalog_Org (but index data in Catalog_Org is not changed[!]), and it changes data displayed in ../propertysheets of the given ZClass instances. I believed that "l=i.userid" will put a copy of userid metadata into "l", and that anything I will do with "l" will not influence anything except "l". Why is it behaving like that? Zope 2.4.3, Linux -- Milos Prudek
Milos Prudek wrote:
I think I'm going slightly mad :-)
OK, I tracked it down to what amounts to IMHO a Python feature :-O
x=['a','b','c'] x ['a', 'b', 'c'] y=x y ['a', 'b', 'c'] y.remove('b') y ['a', 'c'] x ['a', 'c']
This problem applies to lists only... a simple assignment seems to assign a reference to original variable instead of creating a copy of original variable data. I use Python 2.1.1 on Linux. Is this normal Python behaviour? -- Milos Prudek
Milos Prudek wrote:
Milos Prudek wrote:
I think I'm going slightly mad :-)
OK, I tracked it down to what amounts to IMHO a Python feature :-O
x=['a','b','c'] x ['a', 'b', 'c'] y=x y ['a', 'b', 'c'] y.remove('b') y ['a', 'c'] x ['a', 'c']
This problem applies to lists only... a simple assignment seems to assign a reference to original variable instead of creating a copy of original variable data.
I use Python 2.1.1 on Linux.
Is this normal Python behaviour?
this is essential python behaviour. python is a language with "pointer semantics", meaning a name is just a pointer to an object (containig value & class/type) infos. see the difference between mutable/immutable objects. in pointer semantic languages, object comparison differentiates between equal value or equal value and structure. Assignment just lets your left hand ident refer to the rhs object. read beazly's essential reference. AND: from copy import copy, deepcopy. -- ------------------------------------------------------------- Who's got only a hammer sees the world as a nail hans augustin (software developer) hans@beehive.de beehive elektronische medien GmbH http://www.beehive.de phone: +49 30 847-82 0 fax: +49 30 847-82 299
This problem applies to lists only... a simple assignment seems to assign a reference to original variable instead of creating a copy of original variable data. Is this normal Python behaviour?
this is essential python behaviour.
OK. Thank you for your help, Hans!
AND: from copy import copy, deepcopy.
Good tip. It is also possible to assign "y=x[0:]", and voila, y is data copy, not a reference, and it is independent. -- Milos Prudek
Milos Prudek wrote:
Good tip. It is also possible to assign "y=x[0:]", and voila, y is data copy, not a reference, and it is independent.
An ordinary copy by assignment goes only 1 level deep. if the objects nest deeper, use deepcopy to get a true copy. ------------------------------------------------------------- Who's got only a hammer sees the world as a nail hans augustin (software developer) hans@beehive.de beehive elektronische medien GmbH http://www.beehive.de phone: +49 30 847-82 0 fax: +49 30 847-82 299
participants (2)
-
hans -
Milos Prudek