Delete a instance passed by reference into a method

Carsten Haese carsten at uniqsys.com
Tue Dec 4 08:47:20 EST 2007


On Mon, 2007-12-03 at 18:27 -0800, hdante wrote:
> (note, you don't want to do this, it's a proof of concept)
> 
> import sys
> 
> class A(object):
> 	def __init__(self):
> 		pass
> 	def m1(self, x = None):
> 		if x == None:
> 			x = sys._getframe(1).f_locals
> 		ab = 'aB'
> 		x[ab].i = 10
> 		del x[ab]
> 		print 'No more B'
> class B(object):
> 	def __init__(self, i):
> 		self.i = i
> 	def __del__(self):
> 		print 'delete B'
> 
> aA = A()
> aB = B(i = 6)
> print str(aB.i)
> aA.m1()
> print str(aB.i)

That's not much of a proof of anything. It only works because the last
block happens to only use globals. If you stick it inside a function
with local names, it'll cease to "work".

The bottom line is that you can not modify the namespace of the caller
within a function, unless you only use globals, and I hope I don't have
to tell you what a fundamentally bad idea that is.

My question to the OP is, what are you actually trying to accomplish?

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list