473,387 Members | 1,492 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,387 software developers and data experts.

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

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

Similar topics

5
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
1
by: leslie_tighe | last post by:
Hello, I have webservice created with Axis 1.2.1 and that I am trying to consuming in .NET (VB) using the Microsoft provided tools. While I am able to consume methods on the service that return...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
8
by: a | last post by:
I'm trying to save data from a custom object into the profile object, but it is not structured the way that I want. I'm trying to get the custom object to serialize as xml to a Profile object...
0
by: a | last post by:
I need to create an instance of a custom object 'School.Teacher' and use it in a Profile object. I'm developing a bad case of "Pretzel Logic" thinking about this. Filling the custom object ...
13
by: andrea | last post by:
Sorry for the stupid question, I know, but sometimes is necessary starts from the basic. I don't know how to pass the result of a method generated from a DAL class to a BL class returning the...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
14
by: Philipp Reif | last post by:
Hi all, I've got a little hole in my head concerning references. Here's what I'm trying to do: I'm calling a function, passing the reference of a business object for editing. The function clones...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...

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.