Odd behavior regarding a list

Dave Angel davea at dejaviewphoto.com
Thu Mar 26 17:15:29 CET 2009


But you didn't do an assignment, you did an append.  Append modifies the 
object, which is referenced by both parent_struct_sig and 
this_cdata["struct-sig"]

If you're sure you want a *copy* of the list before the modifications, 
you might do something like

		parent_struct_sig = \
				self.__class_data[this_class]["struct-sig"][:]


If the items in the list might be modified as well, then you might have 
to do something like  copy.deepcopy()


Edd Barrett wrote:
> Hi there,
>
> My first post here, so hello :)
>
> Just a little background, I am writing my dissertation, which is a JIT
> compiler based upon LLVM and it's python bindings, along with the
> aperiot LL(1) parser.
>
> I have some code here, which is not behaving as I would expect. Could
> someone enlighten me as to why this is so:
>
> ---8<---
> 	def __classdef_integer(self):
> 		"""
> 		class definition for a integer object.
> 		"""
>
> 		this_class = "Integer"
>
> 		# metadata inherited
> 		self.__class_data[this_class] = \
> 				self.__class_data["Object"]
>
> 		# cache parent struct sig for later (when casting ptrs)
> 		parent_struct_sig = \
> 				self.__class_data[this_class]["struct-sig"]
>
> 		this_cdata = self.__class_data[this_class]
> 		#this_cdata["membernames"].append("value")
> 		#this_cdata["type-sym"] = 1
> 		#this_cdata["parent-class"] = "Object"
>
> 		#vtab = self.__construct_vtable(this_class)
> 		#self.__ir_add_typesym(this_class, vtab)
>
>
> 		print "parent::::"
> 		print parent_struct_sig
> 		this_cdata["struct-sig"].append(self.__int_t)
> 		print "new::::"
> 		print parent_struct_sig
>
> 		sys.exit(1)
>  ---8<---
>
> Notice how I am caching the old value of 'parent_struct_sig' before I
> manipulate it.
>
> Produces the following output:
>
> ---8<---
> parent::::
> [<llvm.core.PointerType object at 0x7fcdfdac>]
> new::::
> [<llvm.core.PointerType object at 0x7fcdfdac>, <llvm.core.IntegerType
> object at 0x8231520c>]
> ---8<---
>
> My question is: why has 'parent_struct_sig' changed? I was under the
> impression the assignment operator copies, not references.
>
> Sorry if the answer is blindingly obvious. I am somewhat jaded from
> working on this every day :P
>
> Heres my python environment:
> ---8<---
> Python 2.6.1 (r261:67515, Dec 27 2008, 16:56:14)
> [GCC 4.2.0 20070307 (prerelease)] on openbsd4
> Type "help", "copyright", "credits" or "license" for more information.
> ---8<---
>
> Thanks
>
>   



More information about the Python-list mailing list