473,566 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1699
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***@brunningo nline.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
6901
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 a type mismatch. It is positively because of the boolean(java primitive)parameter. It goes fine if I change this parameter to int or String. This...
6
22499
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 instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was...
10
1923
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"> <input type="hidden" name="thisPage" value="pispCheckDomain"> <input type="hidden" name="username" value="test"> <input type="hidden"...
4
5676
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 *************************************************************************************************** DirectoryEntry AD = new DirectoryEntry("WinNT://" + sServerName + ",Computer",UserName,Password); DirectoryEntry admGroup =...
0
1536
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 databases in that server. Then after the user selects a database, I have another button that he/she click and I want to retrieve file groups from a...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.