newbee: adress of object

Steve Holden sholden at holdenweb.com
Mon Aug 5 14:17:32 CEST 2002


"Alexander Eisenhuth" <stacom at stacom-software.de> wrote in message
news:3D4E4CE3.60207 at stacom-software.de...
> hallo together,
>
> for tracing i need to access the objects adress. I thougt about:
>
> class X:
>
> ...
>
>     log (self):
>       print 'myaddr:%s ...' % (self,...)
>

1. Nope, that will print the object's repr(), which foir a classic-class
instance will be something like  "<__main__.A instance at 0x100fc828>". You
could, of course, write a function to *parse* that and extract the address.

2. If you don't mind relying on what's currently an implementation detail,
for which I don't believe there are any guarantees of forward compatibilty,
you might notice that

>>> "address %s" % a
'address <__main__.A instance at 0x100fc828>'
>>> id(a)
269469736
>>> hex(id(a))
'0x100fc828'

In other words, the id() of an object is exactly what you are looking for.

still-not-sure-why-you-need-the-address-though-ly y'rs  - steve
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------






More information about the Python-list mailing list