By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,551 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

getting an object name

P: n/a
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)

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
rk***@ucsd.edu

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

Jul 19 '05 #2

P: n/a
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)


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
Jul 19 '05 #3

P: n/a
On 6/22/05, David Bear <Da********@asu.edu> 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?


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!"

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 19 '05 #4

P: n/a
On Wed, 22 Jun 2005 09:00:23 +0100, rumours say that Simon Brunning
<si************@gmail.com> might have written:
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:


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?
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 19 '05 #5

P: n/a
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

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.