Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60185
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python@mrabarnett.plus.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'variables': 0.07; 'subject:using': 0.09; 'python': 0.11; 'def': 0.12; '"a"': 0.16; '"b"': 0.16; 'a(object):': 0.16; 'attr': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'function?': 0.16; 'locals():': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'subject:member': 0.16; 'type),': 0.16; '{"a":': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'variable': 0.18; '(but': 0.19; 'header:User-Agent:1': 0.23; 'define': 0.26; 'pass': 0.26; 'subject:/': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; '[1]': 0.29; '[2]': 0.30; 'bigger': 0.30; "i'm": 0.30; 'code': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'var': 0.36; 'being': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'name': 0.63; 'different': 0.65; 'header:Reply- To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'catherine': 0.84; 'reply-to:addr:python.org': 0.84; 'vars': 0.91 |
| X-CM-Score | 0.00 |
| X-CNFS-Analysis | v=2.1 cv=C6LQl2/+ c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=OJn0C7AadrgA:10 a=XhCaLfk33RcA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=EKjCzSmYASUA:10 a=h_fDB0RM9R16XhkS6lgA:9 a=wPNLvfGTeEIA:10 |
| X-AUTH | mrabarnett:2500 |
| Date | Fri, 22 Nov 2013 00:52:21 +0000 |
| From | MRAB <python@mrabarnett.plus.com> |
| User-Agent | Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.1.1 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: using getattr/setattr for local variables in a member function |
| References | <l6m40a$9vd$1@news.jpl.nasa.gov> |
| In-Reply-To | <l6m40a$9vd$1@news.jpl.nasa.gov> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| Reply-To | python-list@python.org |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3021.1385081545.18130.python-list@python.org> (permalink) |
| Lines | 52 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1385081545 news.xs4all.nl 15936 [2001:888:2000:d::a6]:37945 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:60185 |
Show key headers only | View raw
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++!)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web