2
class Bar:
     pass

class Foo:
     def __str__(self): return "Foo instance"

>> aBar = Bar()
>> print aBar
<__main__.Bar instance at 0x100572a28>
>> aFoo = Foo()
>> print aFoo
Foo instance

is there a way to print out the address of aFoo after overriding the str method?

using

 >>repr(aFoo) 

solved my problem

Matt
  • 22,721
  • 17
  • 71
  • 112
Doboy
  • 10,411
  • 11
  • 40
  • 48

1 Answers1

6

At least in cpython, id provides the address. But the output is in decimal; you have to convert that to hex:

>>> f = (x for x in [1,2,3])
>>> print f
<generator object <genexpr> at 0x1004d22d0>
>>> '%x' % id(f)
'1004d22d0'

Actually, though, the __repr__ function isn't altered when __str__ is overridden. So you can do this as well:

>>> class Foo:
...     def __str__(self): return "Foo instance"
... 
>>> a = Foo()
>>> print a
Foo instance
>>> print repr(a)
<__main__.Foo instance at 0x1004d1c68>

I think id is preferable for this, if what you want is really the id. But id is not guaranteed to return the address; that's just the cpython implementation. I don't know whether it's specified that the built-in __repr__ of objects has to return an address, or whether it has to return the id, or neither. So if you specifically want whatever it is that __repr__ provides, then this may be the way to go.

Update: The answer is neither, at least according to the language reference, which dictates only that the __repr__ of an object be "information-rich and unambiguous." And indeed, sometimes the __repr__ does not actually return the address of the specific object in question, as seen here:

>>> a = Foo()
>>> '%x' % id(a)
'1004d1fc8'
>>> '%x' % id(a.__str__)
'1004745a0'
>>> '%x' % id(Foo.__str__)
'1004745a0'
>>> repr(a.__str__)
'<bound method Foo.__str__ of <__main__.Foo instance at 0x1004d1fc8>>'
senderle
  • 145,869
  • 36
  • 209
  • 233
  • On .Net objects don't have a fixed address so there's no way that the IronPython repr can include an address: .Net garbage collection moves objects around so that free memory is nearly contiguous. I'm not sure about Jython but it may well be the same. – Duncan Jun 02 '11 at 20:43
  • @Duncan, I thought as much. Not having an IronPython installation to test, what does IronPython provide in default `__repr__` methods? Is it the same as the value returned by `id`? – senderle Jun 02 '11 at 21:53
  • yes it is the value returned by `id()` but the id isn't assigned until `id()` is called and is a simple increasing count. For example I just created 4 instances (a,b, c, d) of a class then printed their `repr()` in arbitrary order (d, b, c, a) and the ids match the order in which I printed them: `id(a), id(b), id(c), id(d)` gives `(48, 46, 47, 45)`. – Duncan Jun 03 '11 at 08:40
  • @Duncan, thanks -- good to know. I guess lazy assignment of ids is an optimal approach in that case, although I think it gently stretches the definition of `id` as providing an id number that is "unique and constant for this object during its lifetime." – senderle Jun 06 '11 at 02:14