Printable string for 'self'

Terry Reedy tjreedy at udel.edu
Tue Mar 14 22:35:20 EST 2006


"Don Taylor" <nospamformeSVP at gmail.com> wrote in message 
news:dv7sjt$7rb$1 at sea.gmane.org...
> Is there a way to discover the original string form of the instance that
> is represented by self in a method?
> For example, if I have:
> fred = C()
> fred.meth(27)
> then I would like meth to be able to print something like:
> about to call meth(fred, 27) or
> about to call fred.meth(27)
> instead of:
> about to call meth(<__main__.C instance at 0x00A9D238>, 27)

That last *is* the default 'original string form of the instance'. 
Consider
>>> [C() for i in [1,2,3]]
[<__main__.C object at 0x00B536F0>, <__main__.C object at 0x00B53D90>, 
<__main__.C object at 0x00B53830>]

If you want your instances to have a name attribute, as with functions, 
classes, and modules, then give them one and supply it to the init method. 
Note that it does not have to be a bindable name, just a string.  IE, 
'a[1]' could be the string identifier.

Terry Jan Reedy






More information about the Python-list mailing list