Re: getting an object name

2005-06-22 Thread Irmen de Jong
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting an object name

2005-06-22 Thread Simon Brunning
On 6/22/05, David Bear [EMAIL PROTECTED] 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,
[EMAIL PROTECTED],
http://www.brunningonline.net/simon/blog/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting an object name

2005-06-22 Thread TZOTZIOY
On Wed, 22 Jun 2005 09:00:23 +0100, rumours say that Simon Brunning
[EMAIL PROTECTED] 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...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting an object name

2005-06-22 Thread Ron Adam
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

-- 
http://mail.python.org/mailman/listinfo/python-list