Odd behavior regarding a list

Albert Hopkins marduk at letterboxes.org
Thu Mar 26 12:00:46 EDT 2009


On Thu, 2009-03-26 at 08:36 -0700, 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.

No, assignment is basically giving a (different) name to an object.  So,
e.g.:

>>> x = [] # RHS creates a new object, LHS says, let x refer to this
object
>>> id(x) # id of object that x refers to
3087260396L
>>> y = x # RHS returns to an object, LHS says let "y" refer to this
object
>>> id(y)
3087260396L
>>> # See that x and y are referencing the same object
>>> x.append(1)
>>> x
[1]
>>> y
[1]








More information about the Python-list mailing list