Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60183 > unrolled thread

using getattr/setattr for local variables in a member function

Started byCatherine M Moroney <Catherine.M.Moroney@jpl.nasa.gov>
First post2013-11-21 15:12 -0800
Last post2013-11-22 17:07 +1300
Articles 6 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  using getattr/setattr for local variables in a member function Catherine M Moroney <Catherine.M.Moroney@jpl.nasa.gov> - 2013-11-21 15:12 -0800
    Re: using getattr/setattr for local variables in a member function MRAB <python@mrabarnett.plus.com> - 2013-11-22 00:52 +0000
    Re: using getattr/setattr for local variables in a member function Ned Batchelder <ned@nedbatchelder.com> - 2013-11-21 17:58 -0800
    Re: using getattr/setattr for local variables in a member function Dave Angel <davea@davea.name> - 2013-11-21 21:02 -0500
    Re: using getattr/setattr for local variables in a member function Ethan Furman <ethan@stoneleaf.us> - 2013-11-21 19:18 -0800
    Re: using getattr/setattr for local variables in a member function Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-11-22 17:07 +1300

#60183 — using getattr/setattr for local variables in a member function

FromCatherine M Moroney <Catherine.M.Moroney@jpl.nasa.gov>
Date2013-11-21 15:12 -0800
Subjectusing getattr/setattr for local variables in a member function
Message-ID<l6m40a$9vd$1@news.jpl.nasa.gov>
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

[toc] | [next] | [standalone]


#60185

FromMRAB <python@mrabarnett.plus.com>
Date2013-11-22 00:52 +0000
Message-ID<mailman.3021.1385081545.18130.python-list@python.org>
In reply to#60183
On 21/11/2013 23:12, Catherine M Moroney wrote:
> 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++!)

[toc] | [prev] | [next] | [standalone]


#60188

FromNed Batchelder <ned@nedbatchelder.com>
Date2013-11-21 17:58 -0800
Message-ID<861a5662-7d44-414a-9243-a2c0ed70b267@googlegroups.com>
In reply to#60183
On Thursday, November 21, 2013 6:12:10 PM UTC-5, Catherine M Moroney wrote:
> 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 much more 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.

[toc] | [prev] | [next] | [standalone]


#60190

FromDave Angel <davea@davea.name>
Date2013-11-21 21:02 -0500
Message-ID<mailman.3023.1385085707.18130.python-list@python.org>
In reply to#60183
On Fri, 22 Nov 2013 00:52:21 +0000, MRAB <python@mrabarnett.plus.com> 
wrote:
> > 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?

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.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#60195

FromEthan Furman <ethan@stoneleaf.us>
Date2013-11-21 19:18 -0800
Message-ID<mailman.3024.1385092281.18130.python-list@python.org>
In reply to#60183
On 11/21/2013 06:02 PM, Dave Angel wrote:
> Catherine Moroney wrote:
>>
>> 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?
>
> 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.

--
~Ethan~

[toc] | [prev] | [next] | [standalone]


#60198

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2013-11-22 17:07 +1300
Message-ID<bf83knFu0s8U1@mid.individual.net>
In reply to#60183
Catherine M Moroney wrote:
> 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.

-- 
Greg

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web