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?

kramer65
  • 50,427
  • 120
  • 308
  • 488

3 Answers3

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'
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 1
    Does this mean that what I am trying to do is impossible? – kramer65 Jun 19 '13 at 19:58
  • @kramer65 yes, you can't get a variable name in python. They are just references to access an object. – Ashwini Chaudhary Jun 19 '13 at 19:59
  • 1
    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). – chepner Jun 19 '13 at 20:04
  • 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 Jun 19 '13 at 20:04
  • @kramer65 I've added a simple workaround solution. – Ashwini Chaudhary Jun 19 '13 at 20:08
  • 1
    @kramer65 The solution to your problem is to use a debugger, not gum up your code – Marcin Jun 19 '13 at 20:09
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.

Marcin
  • 48,559
  • 18
  • 128
  • 201
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.

dawg
  • 98,345
  • 23
  • 131
  • 206
  • 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. – AbdurRehman Khan Apr 03 '19 at 10:50