using getattr/setattr for local variables in a member function

  • Thread starter Catherine M Moroney
  • Start date
C

Catherine M Moroney

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
 
M

MRAB

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?
You can get the local names of a function using locals():

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

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

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

for name in ("a", "b"):
var = locals()[name]
var.append(100)

BTW, in Python they're called "methods". (C++ calls them "member
functions", but Python isn't C++!)
 
N

Ned Batchelder

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

Catherine, it's a little hard to know what your real code is doing from this toy sample. Usually, if you want to work with "variable variables" as you're suggesting here, the better approach is to use one dictionary instead of many variables. Then you can deal with them as a single collection muchmore conveniently.

Can you show us the real code in question? It will be much easier to make a good recommendation if we can see what you are doing with these variables..

--Ned.
 
D

Dave Angel

If you mean to access them from within the same method, someone else
has already shown it using locals(). But you cannot access locals
from a method that's already terminated. They no longer exist.
 
E

Ethan Furman

If you mean to access them from within the same method, someone
else has already shown it using locals(). But you cannot access
locals from a method that's already terminated. They no longer exist.

Also, accessing is fine, but not all pythons support changing them.
 
G

Gregory Ewing

Catherine said:
is there
some way to use getattr/setattr to access the local variables specific
to a given function?

No, because those variables don't even exist when there
isn't a call to the function in progress.

Your example suggests that, instead of local variables,
you really want them to be attributes of your object
somehow. The best way to go about that will depend on
how you want to use them.

If you explain more about the problem you're trying
to solve, we may be able to suggest a solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

recursion error using setattr and getattr 0
dynamic setattr 2
getattr/setattr q. 10
extended setattr() 4
setattr getattr confusion 7
setattr question 1
Use cases for "setattr" in existing code 0
extend getattr() 3

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top