Paramaterized local variable name

Hans Nowak wurmy at earthlink.net
Sun Dec 30 00:30:12 EST 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



More information about the Python-list mailing list