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