getting an object name

D

David Bear

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)
 
R

Robert Kern

David said:
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
(e-mail address removed)

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

Irmen de Jong

David said:
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)

You don't, see the other reply.

You didn't say why you think you need this for, but I suspect
that you can solve your case by using a dict in one way or another:

{ "somename": [1,2,3,4,5] }


--Irmen
 
S

Simon Brunning

Let's say I have a list called, alist. If I pass alist to a function,
how can I get the name of it?

The effbot put it beautifully:

"The same way as you get the name of that cat you found on your porch:
the cat (object) itself cannot tell you its name, and it doesn't
really care -- so the only way to find out what it's called is to ask
all your neighbours (namespaces) if it's their cat (object) ... and
don't be surprised if you'll find that it's known by many names, or no
name at all!"
 
C

Christos TZOTZIOY Georgiou

The effbot put it beautifully:

And IMO it should be in the FAQ:
(http://www.python.org/doc/faq/general.html)

"How do I get the name of an object?"
"The same way as you get the name of that cat you found on your porch:
the cat (object) itself cannot tell you its name, and it doesn't
really care -- so the only way to find out what it's called is to ask
all your neighbours (namespaces) if it's their cat (object) ... and
don't be surprised if you'll find that it's known by many names, or no
name at all!"

Whom should we bug to add it?
 
R

Ron Adam

David said:
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)

Using an object's name as data isn't a good idea because it will
generally cause more problems than it solves.


If you have several different types of lists and need to handle them
differently, then you might consider using class's that knows how to
handle each type of list.

Also, if the name is really part of the data, then you should store the
name as a string with the list.


class data1(object):
def __init__(self, alist, name):
self.list = alist
self.name = name
def data_type(self):
print "This is a data1 object"

class data2(object):
def __init__(self, alist, name):
self.list = alist
self.name = name
def data_type(self):
print "This is a data2 object"

list_a = data1( range(10), "small list")
print list_a.list
print list_a.name

list_b = data2( range(100), "large list")

def data_type(data):
data.data_type() # don't need the name here

data_type(list_a) # prints 'This is a data1 object'
data_type(list_b) # prints 'This is a data2 object'



You can also store a name with the list in a list if you don't want to
use class's.

alist = ['mylist',range[10]]
print alist[0] # name
print alist[1] # list


Regards,
Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top