access method local variable namespace?

emile at emile at
Thu May 11 10:13:19 EDT 2000

Accessing an instances' values is rather easy, but if I get your
meaning, you want to access, eg, local_var from the __init__ 
method in the sample code below?  I don't know how you'd get
to it without saving a reference to it somewhere.  Maybe
someone can point out a way if I'm wrong.  Of course, the
easy way would be to make it an instance variable.

>>> class A:
	def __init__(self,val):
		self.val = val
		local_var = val*2
>>> a = A(3)
>>> a.__dict__['val']

Emile van Sebille
emile at

Douglas du Boulay <ddb at> wrote in message
news:<391A3F65.54AC9105 at>...
> hi,
> I know it's possible to access the names of methods within 
> class objects from their __class__.__dict__, but I am 
> wondering if it's also possible to 
> access the namespace of the local variables in those 
> methods and to read their contents?
> >From the "Programming Python" book I read that local variables are
> stored in an array instead of a dictionary for faster access and that
> vars() and locals() can be used to access those variables when 
> they are in the current scope, but is it possible to this when they
> are out of scope?
> Thanks
> doug
> -- 

More information about the Python-list mailing list