getting an object name

Robert Kern rkern at ucsd.edu
Wed Jun 22 02:37:50 EDT 2005


David Bear wrote:
> Let's say I have a list called, alist. If I pass alist to a function,
> how can I get the name of it?
> 
> alist = range(10)
> 
> def afunction(list):
>     listName = list.__name__ (fails for a list object)

In general, you don't. A particular object can have any number of names 
in various scopes or have no name at all. In particular, in your code, 
the same list object has 2 names in 2 scopes, "alist" in the global 
scope and "list" in afunction's local scope.

alist = range(10)
blist = alist
clist = [alist]

alist is blist
alist is clist[0]

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list