Odd behavior regarding a list

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Mar 26 17:21:56 CET 2009


On Thu, 26 Mar 2009 08:36:49 -0700, Edd Barrett wrote:

> Hi there,
> 
> My first post here, so hello :)

Hello and welcome. Let me comment on a few things out of order.


> My question is: why has 'parent_struct_sig' changed? I was under the
> impression the assignment operator copies, not references.

You are mistaken. Firstly, Python does not have an assignment operator. 
Assignment (technically, name binding) is not an operator in Python. You 
can't over-ride it.

Secondly, assignment in Python *never* copies. Ever. The only time a copy 
is made is when you explicitly ask for a copy.

e.g. to copy a list L, use one of:

list(L)

L[:]

import copy
copy.copy(L)

import copy
copy.deepcopy(L)



A few more comments, based on your code.


> 	def __classdef_integer(self):

Double-underscore name mangling is often more trouble than it is worth. 
Unless you really need it, not just think you will need it, it is 
generally considered poor style.


...
> 		# cache parent struct sig for later (when casting ptrs)

If you're actually casting pointers in your code, I don't know what 
language you're using, but it isn't Python! Python is not C, you don't 
have direct access to pointers. I don't know what you're doing, perhaps 
the comment is misleading?


...
> 		sys.exit(1)

You do realise that this line tells your program to exit? Seems like a 
strange thing to include in a method.

> Notice how I am caching the old value of 'parent_struct_sig' before I
> manipulate it.

Yes. You cache it, and then you modify the object in the cache. 



Hope some of this is helpful.



-- 
Steven




More information about the Python-list mailing list