REPOST: Re: Paramaterized local variable name

Hans Nowak wurmy at earthlink.net
Sun Dec 30 06:30:12 CET 2001


Tim Cera wrote:
> 
> Hello,
> 
> I have the following code:
> 
> def paging(data, key, query_dict):
>   for i in range(0, len(data[key]), use_pagesize):
>     start = data[key][i].name
>   ...etc.
> 
> which works fine for data[key][i].name, but sometimes I want this
> function to process data[key][i].rev.
> 
> So, if local_name is equal to 'name' or 'rev' or 'whatever', what I
> want is something like:
> 
> def paging(data, key, query_dict, local_name):
>   for i in range(0, len(data[key]), use_pagesize):
>     start = data[key][i].
> #       What goes here   ^^^^  ?

You probably want

  start = getattr(data[key][i], local_name)

Now you can set local_name to a string "name" or "rev"
or whatever attribute of data[key][i] you want to
access.

--Hans

========= WAS CANCELLED BY =======:
Path: news.sol.net!spool0-nwblwi.newsops.execpc.com!newsfeeds.sol.net!news-out.visi.com!hermes.visi.com!news.maxwell.syr.edu!feeder.kornet.net!news1.kornet.net!ua4canc3ll3r
From: Hans Nowak <wurmy at earthlink.net>
Newsgroups: comp.lang.python
Subject: cmsg cancel <3C2EA509.63ACDDC9 at earthlink.net>
Control: cancel <3C2EA509.63ACDDC9 at earthlink.net>
Date: Mon, 31 Dec 2001 02:20:33 GMT
Organization: A poorly-installed InterNetNews site
Lines: 2
Message-ID: <cancel.3C2EA509.63ACDDC9 at earthlink.net>
NNTP-Posting-Host: 211.57.49.2
X-Trace: news2.kornet.net 1009774382 27193 211.57.49.2 (31 Dec 2001 04:53:02 GMT)
X-Complaints-To: usenet at news2.kornet.net
NNTP-Posting-Date: Mon, 31 Dec 2001 04:53:02 +0000 (UTC)
X-No-Archive: yes
X-Unac4ncel: yes
X-Commentary: I love NewsAgent 1.10 and the Sandblaster Cancel Engine Build 74 (19 March 1999)

This message was cancelled from within Mozilla.



More information about the Python-list mailing list