[Tutor] Re: List's name in a string

Andrei project5 at redrival.net
Mon Sep 29 19:21:12 EDT 2003


Héctor Villafuerte D. wrote:

Hi,

> is there a way to get a list's (or any variable) name in a string?
> 
> For example:
>  >>> list1 = [1, 2, 3]
> I would like to do something like this:
>  >>> var = list1.name()
> So that  var  contains  'list1'.

Well, there's something odd about Python here:

 >>> a = b = [5,4]
 >>> a.append(4)
 >>> a, b
([5, 4, 4], [5, 4, 4])

So, which name would you like to get when you look up [5,4]? a or b or both?
The closest thing I can think of is using the locals() dictionary, like this:

 >>> for key in loc:
...     if loc[key] == [5,4,4]:
...         print key,
...
a b

You can do some tweaking, like only get the first match, or don't get duplicates 
if they have the same id().

-- 
Yours,

Andrei

=====
Mail address in header catches spam. Real contact info (decode with rot13):
cebwrpg5 at bcrenznvy.pbz. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq gur 
yvfg, fb gurer'f ab arrq gb PP.





More information about the Tutor mailing list