473,382 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

getting an object name

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
5 1692
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
10
by: Peter Afonin | last post by:
Hello, I have a simple client-side form that is checking the domain availability on the domain registrar's server: <FORM action="https://www.webnames.ru/scripts/RegTimeSRS.pl" method="post">...
4
by: shashank kadge | last post by:
hi all, i am trying to get local admin users and groups on a windows server. here is the C# code that i am using...
0
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.