4

I've got a simple class from which I create two objects. I now want to print the name of the object from within the class. So something like this:

class Example: def printSelf(self): print self object1 = Example() object2 = Example() object1.printSelf() object2.printSelf() 

I need this to print:

object1 object2 

Unfortunately this just prints <myModule.Example instance at 0xb67e77cc>

Does anybody know how I can do this?

3
  • By name of the object, do you mean - name of the reference (object1, object2, ...)?? Commented Jun 19, 2013 at 19:53
  • Yes indeed. Excuse me for not being explicit on this, I'll edit my question on that. Commented Jun 19, 2013 at 19:55
  • See my original post here: stackoverflow.com/a/59364138/5088165 Commented Apr 20, 2020 at 19:19

3 Answers 3

5

object1 is just an identifier(or variable) pointing to an instance object, objects don't have names.

>>> class A: ... def foo(self): ... print self ... >>> a = A() >>> b = a >>> c = b >>> a,b,c #all of them point to the same instance object (<__main__.A instance at 0xb61ee8ec>, <__main__.A instance at 0xb61ee8ec>, <__main__.A instance at 0xb61ee8ec>) 

a,b,c are simply references that allow us to access a same object, when an object has 0 references it is automatically garbage collected.

A quick hack will be to pass the name when creating the instance:

>>> class A: ... def __init__(self, name): ... self.name = name ... >>> a = A('a') >>> a.name 'a' >>> foo = A('foo') >>> foo.name 'foo' >>> bar = foo # additional references to an object will still return the original name >>> bar.name 'foo' 
Sign up to request clarification or add additional context in comments.

6 Comments

Does this mean that what I am trying to do is impossible?
@kramer65 yes, you can't get a variable name in python. They are just references to access an object.
I think you mean objects don't have names; a variable consists of a name and a value (although it is more correct to say that Python has identifiers which are bound to objects than to say it has variables).
Alright. That's a pity. I just have two objects created from the same class. In one of them something seems to go wrong. So to debug I want to print some info from one of the objects, but not from the other one. That's why I wanted to do an if self == 'someObject': print 'someinfo'. But in that case I'll have to find a different solution. Thanks!
@kramer65 The solution to your problem is to use a debugger, not gum up your code
|
4

The object does not have a "name". A variable which refers to the object is not a "name" of the object. The object cannot know about any of the variables which refer to it, not least because variables are not a first-class subject of the language.

If you wish to alter the way that object prints, override either __repr__ or __unicode__.

If this is for debugging purposes, use a debugger. That's what it's for.

Comments

1

The common way to do this is something along these lines:

class Example(object): def __init__(self,name): self.name=name def __str__(self): return self.name object1 = Example('object1') object2 = Example('object2') print object1 print object2 

Prints:

object1 object2 

However, there is no guarantee that this object remains bound to the original name:

object1 = Example('object1') object2 = object1 print object1 print object2 

Prints object1, as expected, twice. If you want to see things under the hood -- use a debugger.

1 Comment

Don't you need to print object1.name and object2.name!? EDIT: Didn't notice the __str__ dunder, not sure when that gets invoked though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.