using getattr/setattr for local variables in a member function

Catherine M Moroney Catherine.M.Moroney at jpl.nasa.gov
Thu Nov 21 18:12:10 EST 2013


Hello,

If I have a class that has some member functions, and all the functions 
define a local variable of the same name (but different type), is there
some way to use getattr/setattr to access the local variables specific 
to a given function?

Obviously there's no need to do this for the small test case below,
but I'm working on a bigger code where being able to loop through
variables that are local to func2 would come in handy.

For example:

class A(object):
    def __init__(self):
	pass

    def func1(self):
	a = {"A": 1}
	b = {"B": 2}

    def func2(self):
         a = [1]
	b = [2]

	for attr in ("a", "b"):
	   var = getattr(self, attr)  ! What do I put in place of self
	   var.append(100)            ! to access vars "a" and "b" that
                                       ! are local to this function?

Catherine



More information about the Python-list mailing list