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

How to get an object's name as a string?

P: n/a
I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.

How would one extract the name of an object from an object instance as
a string. I would think that it is stored as an attribute of the
object but successive 'dir()' calles haven't found me the attribute
with the namestring.

My thanks!

Oct 28 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Shannon Mayne wrote:
I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.

How would one extract the name of an object from an object instance as
a string. I would think that it is stored as an attribute of the
object but successive 'dir()' calles haven't found me the attribute
with the namestring.

My thanks!
Once again (there have been many posts on this subject). Objects can have more
than one name in Python. Therefore there is not a one-to-one correspondence
between an object instance and name(s) that point to it.

Example:

a = myObject()
b = a
c = a

now a, b, c all point to the same object. How would you define it's "name".

You are certainly free to store a name as an attribute to the object, but the
linkage would then be purely logical.

Example:

objects = []
objects.append(myObject('a'))
#
# Find object with name == 'a'
#
obj = None
for object in objects:
if object.name == 'a':
obj = object
-Larry
Oct 28 '08 #2

P: n/a
On Oct 28, 2008, at 8:41 AM, Shannon Mayne wrote:
I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.
What do you mean by the "name" of an object? Objects don't generally
have names, unless you explicitly define a .name property and assign
them names.

(Variables have names, of course, but a variable isn't an object --
it's just a reference to an object. Many variables may refer to the
same object, so it doesn't make any sense to ask for the name of THE
variable which may be referring to an object at the moment.)
How would one extract the name of an object from an object instance as
a string. I would think that it is stored as an attribute of the
object but successive 'dir()' calles haven't found me the attribute
with the namestring.
As noted above, there is no built-in name attribute. Define one,
perhaps like this:

class Foo():
def __init__(name):
self.name = name

Now your Foo objects have a name attribute, and if "x" is a reference
to such an object, you would access that as "x.name".

It's still unclear what you intend to do with these, but if at some
point you want to access objects by their names (from user input or
whatever), then you'll also need a dictionary to map names to
objects. So to your __init__ function, you might add something like
this:

name_map[name] = self

where name_map was initialized to {} at the top of the file. Then you
can use name_map to look up any object of this class by name.
Remember that this will keep these objects from automatically
disappearing when there are no other references (other than the map)
to them. If that's a problem, explicitly remove them from the map
when you know you're done with them, or use weak references.

Best,
- Joe

Oct 28 '08 #3

P: n/a
On Tue, 28 Oct 2008 09:15:50 -0600, Joe Strout wrote:
On Oct 28, 2008, at 8:41 AM, Shannon Mayne wrote:
>I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.

What do you mean by the "name" of an object? Objects don't generally
have names, unless you explicitly define a .name property and assign
them names.

(Variables have names, of course, but a variable isn't an object -- it's
just a reference to an object. Many variables may refer to the same
object, so it doesn't make any sense to ask for the name of THE variable
which may be referring to an object at the moment.)
That explanation makes no sense. Given the assignment:

x = 57

if the name of x isn't 'x', then what on earth can it possibly mean to
ask for the name of a variable?

In languages like Python, the term "variable" is misleading and
confusing. Python's programming model has objects (values), and names.
Best to use language that describes what Python actually does, rather
than use language that describes what other languages do.


--
Steven
Oct 28 '08 #4

P: n/a
Steven D'Aprano wrote:
On Tue, 28 Oct 2008 09:15:50 -0600, Joe Strout wrote:
>On Oct 28, 2008, at 8:41 AM, Shannon Mayne wrote:
>>I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.
What do you mean by the "name" of an object? Objects don't generally
have names, unless you explicitly define a .name property and assign
them names.

(Variables have names, of course, but a variable isn't an object -- it's
just a reference to an object. Many variables may refer to the same
object, so it doesn't make any sense to ask for the name of THE variable
which may be referring to an object at the moment.)

That explanation makes no sense. Given the assignment:

x = 57

if the name of x isn't 'x', then what on earth can it possibly mean to
ask for the name of a variable?
He didn't ask for the name of a variable, he asked for the name of an
object. You may choose to equate them, but they aren't the same thing.
In languages like Python, the term "variable" is misleading and
confusing. Python's programming model has objects (values), and names.
Best to use language that describes what Python actually does, rather
than use language that describes what other languages do.
Objects in Python *don't* have names. Period. In Python we don't
normally talk about variables anyway, except when speaking loosely, we
talk about binding names. But please don't let this start another round
of "Python programmers don't know how to describe the language". You
have already made your opinions on that score more than clear.

l = []
l.append(l)
del l

What's the name of the list formerly known as "l"?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Oct 29 '08 #5

P: n/a
Steven D'Aprano wrote:
On Tue, 28 Oct 2008 09:15:50 -0600, Joe Strout wrote:
>On Oct 28, 2008, at 8:41 AM, Shannon Mayne wrote:
>>I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.
What do you mean by the "name" of an object? Objects don't generally
have names, unless you explicitly define a .name property and assign
them names.

(Variables have names, of course, but a variable isn't an object -- it's
just a reference to an object. Many variables may refer to the same
object, so it doesn't make any sense to ask for the name of THE variable
which may be referring to an object at the moment.)

That explanation makes no sense. Given the assignment:

x = 57

if the name of x isn't 'x', then what on earth can it possibly mean to
ask for the name of a variable?
He didn't ask for the name of a variable, he asked for the name of an
object. You may choose to equate them, but they aren't the same thing.
In languages like Python, the term "variable" is misleading and
confusing. Python's programming model has objects (values), and names.
Best to use language that describes what Python actually does, rather
than use language that describes what other languages do.
Objects in Python *don't* have names. Period. In Python we don't
normally talk about variables anyway, except when speaking loosely, we
talk about binding names. But please don't let this start another round
of "Python programmers don't know how to describe the language". You
have already made your opinions on that score more than clear.

l = []
l.append(l)
del l

What's the name of the list formerly known as "l"?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Oct 29 '08 #6

P: n/a
On Oct 28, 2008, at 4:45 PM, Steven D'Aprano wrote:
>What do you mean by the "name" of an object? Objects don't generally
have names, unless you explicitly define a .name property and assign
them names.

(Variables have names, of course, but a variable isn't an object --
it's
just a reference to an object. Many variables may refer to the same
object, so it doesn't make any sense to ask for the name of THE
variable
which may be referring to an object at the moment.)

That explanation makes no sense. Given the assignment:

x = 57

if the name of x isn't 'x', then what on earth can it possibly mean to
ask for the name of a variable?
Perhaps you're skimming rather than reading carefully? Variables do
have names, as I pointed out, and the name of x is indeed 'x'. But
that's not what the OP was asking for -- in your example, he'd be
asking for the name of 57 (expecting it to be 'x'). Numbers don't
have names; objects don't have names; variables have names, and may
refer to numbers or objects.
In languages like Python, the term "variable" is misleading and
confusing.
Oh good grief. Now you're going to try to discard the standard term
"variable" as well?

All right then, if you really insist on making Python more mysterious
by making up new terms for perfectly ordinary and standard programming
concepts, then I suggest the following:

variable: "ariablevay"
value: "aluvay"
reference: "eferencevay"
call-by-value: "allcay-ibay-aluvay"
call-by-reference: (no term needed, since Python doesn't have it)

There. Now we've got a simple mapping from standard terminology to
properly mystical Python-culture terms that are nonetheless easy to
learn. Agreed?

Best,
- Joe

P.S. Shannon: don't listen to Steven. He's out to confuse you and
make Python seem much harder and complex than it really is.

Oct 29 '08 #7

P: n/a
On Oct 28, 2008, at 6:58 PM, Steve Holden wrote:
Objects in Python *don't* have names. Period. In Python we don't
normally talk about variables anyway, except when speaking loosely, we
talk about binding names. But please don't let this start another
round
of "Python programmers don't know how to describe the language". You
have already made your opinions on that score more than clear.
As have I, I suppose, and I'll try to quit engaging in that argument
in the future.
l = []
l.append(l)
del l

What's the name of the list formerly known as "l"?
Hey, that's a very nice little demonstration of an orphaned object.
Thanks for sharing it!

Best,
- Joe

Oct 29 '08 #8

P: n/a
On Oct 29, 12:41*am, Shannon Mayne <shang...@gmail.comwrote:
I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.
The simplest and best option here is to store the objects in a
dictionary with their keys being the generated names.

Oct 29 '08 #9

P: n/a
The simplest and best option here is to store the objects in a
dictionary with their keys being the generated names.
Thanks. Indeed Alex, that may well be the simplest way, to have an
overarching Dictionary of references/names and objects.

However this does not help me to use the reference/name of an object I
imported instead of created.

#-----#

There has been much debate over terminology but I was under the
impression that there was a simple input-output defined task in
question.

Inherently a the variable/name/reference maps to the object it was
assigned to. The question is can one map it the other way? If there
are multiple assignments to the same object can they be listed? This
applies to the "names" object atributes as well.

Joe has mentioned that I shouldn't need to do this and should perhaps
rethink my problem formulation. Nonetheless, I would still like to
know how it might be done.

Thanks
Shannon

Oct 29 '08 #10

P: n/a
On Oct 29, 11:31*pm, ShanMayne <shang...@gmail.comwrote:
However this does not help me to use the reference/name of an object I
imported instead of created.
I've never really understood these requests (and they come up a lot).
Unless you're doing a '*' import, you'll already know the bound names
of the objects you're importing. If you -are- doing a '*' import and
you -don't- know what objects are being imported, how will you refer
to the object to find out its name?

However, maybe one of the following examples will be of use.

Assume a module 'items' that contains:

a = 1
b = 'string'

The best way would be to use the module as it is intended, as a
namespace:
>>import items
names = [x for x in dir(items) if not '__' in x] # ignore
special objects
>>names
['a', 'b']
>>one = getattr(items, names[0])
two = getattr(items, names[1])
one
1
>>two
'string'

Another way is to look through the variables in the current scope:
>>before = set(locals())
before.add('before') # you want to ignore this object too
from items import *
names = list(set(locals()).difference(before))
names
['a', 'b']
>>one = locals()[names[0]]
two = locals()[names[1]]
one
1
>>two
'string'

Do either of these help with your problem?
Oct 29 '08 #11

P: n/a
Indeed they do. My delighted thanks. You have most precisely addressed
the problem I intended to convey.

I should have given the case of module attributes a moments further
thought, an obvious answer. The locals() was unknown to me (rookie
gaps).

Thank you for the elaborated illustration.

good stuff.
Oct 29 '08 #12

P: n/a
Steve Holden <st***@holdenweb.comwrote:
>That explanation makes no sense. Given the assignment:

x = 57

if the name of x isn't 'x', then what on earth can it possibly mean to
ask for the name of a variable?
He didn't ask for the name of a variable, he asked for the name of an
object. You may choose to equate them, but they aren't the same thing.
When I do that assignment there seem to be 5 references to that object, two
of them are dictionary keys (what's the name of a dictionary key?), and two
are dictionary values. the last one is of course x. Any of these seem a
reasonable answer to the question, but how the code is supposed to tell
which name is the one the user wanted is another matter.
>>import varname
x = 57
for s in varname.object_info(x):
.... print s
....
opcode.opmap['INPLACE_MULTIPLY']
encodings.cp850.encoding_map[57]
encodings.cp850.decoding_map[57]
dis.opmap['INPLACE_MULTIPLY']
__main__.x
>>>
When I repeat the experiment in Idle the cp850 encoding entries disappear so
I only get a choice of 3 'names'.
l = []
l.append(l)
del l

What's the name of the list formerly known as "l"?
My code thinks its name is <...>, but then that's just my code:
>>l = []
l.append(l)
for s in varname.object_info(l):
print s
__main__.l
<...>
>>>
Oct 29 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.