470,599 Members | 1,438 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,599 developers. It's quick & easy.

Printable string for 'self'

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)

Thanks in advance,

Don.

Mar 15 '06 #1
4 1250
Don Taylor wrote:
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)

Not a direct answer to your question, but this may be what you want:
If you give class C a __repr__ method you can avoid the default <class
instance at address> string.
class C(object): def __init__(self, name):
self.name = name
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.name)
def meth(self, y):
print 'about to call %r.meth(%r)' % (self, y)
fred = C('Fred')
fred.meth(27) about to call C('Fred').meth(27) def meth2(x, y): print 'about to call meth2(%r, %r)' % (x, y)
meth2(fred, 42) about to call meth2(C('Fred'), 42)
Of course, this doesn't tell you the name of the variable that points
to the instance. For that (here's your direct answer), you will have
to go to the source:
import inspect
import linecache
def f(a, b): print 'function call from parent scope:'
caller = inspect.currentframe().f_back
filename = caller.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, caller.f_lineno)
print ' ' + line.strip()
return a*b
fred = 4
x = f(3, fred)

function call from parent scope:
x = f(3, fred)
But defining __repr__ is far easier and more common.

--Ben

Mar 15 '06 #2
This behavior seems to be commonly wanted by people discovering Python,
and it is the rare case of something one can imagine that is really a
stretch to achieve in Python. Because more or less than one name may
refer to an object, in general an object can't know its name.

You can get part of the way there with

fred = C("fred")

and assign the string to self.name in the constructor. But you must
strictly enforce a convention in your code that the reference fred is
not reassigned to another referent for this to be of much use.

I got in some trouble in these parts a few months back for advocating
some sort of immutable reference, like

fred -> C("fred")

where any reassignment of the refernce during the lifetime of the
referent would raise an exception. This seems to be seen as wrongheaded
by greater pythonistas than myself. I don't fully understand *why*
this is a bad idea, but my intuitive idea that it would be very
valuable has gone away.

mt

Mar 16 '06 #3
Michael Tobis wrote:
I got in some trouble in these parts a few months back for advocating
some sort of immutable reference, like

fred -> C("fred")

where any reassignment of the refernce during the lifetime of the
referent would raise an exception. This seems to be seen as wrongheaded
by greater pythonistas than myself. I don't fully understand *why*
this is a bad idea, but my intuitive idea that it would be very
valuable has gone away.

fred = C("fred")
jim = fred RebindingError: cannot rebind <C object 'fred'> to name "jim" def showit(x): print x

showit(fred) RebindingError: cannot rebind <C object 'fred'> to name "x" fred.amethod() RebindingError: cannot rebind <C object 'fred'> to name "self" fred.aclassmethod() That works! sys.getrefcount(fred) RebindingError: cannot rebind <C object 'fred'> to name "object" del fred


What were you planning to do with this object exactly that didn't involve
binding it to any other names during its lifetime?
Mar 16 '06 #4
> What were you planning to do with this object exactly that didn't involve binding it to any other names during its lifetime?

Nothing so silly as that.

The idea is not to prevent other references from binding to the object,
but to allow the object to ensure that a certain symbol always points
to it.

I no longer suspect that this is a good enough idea to redesign the
language, but it seems to come up regularly.

I will elaborate on my use case later, as I am still looking for a
satisfactory workaround. I think that someone working on a logo-like
platform wanted to be able to assign permanent names to his turtles,
which is an easier case to understand.

In both cases there is the idea to construct a platform for programmers
of intermediate skill, to expose the python environment as well as
powerful objects that they don't need to modify, but need to be able to
access by name. It would be good to be able to guarantee that the name
would be a valid reference to the object in question.

mt

Mar 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Pascal | last post: by
2 posts views Thread by Daniel Alexandre | last post: by
2 posts views Thread by qazmlp | last post: by
8 posts views Thread by Sam Halliday | last post: by
6 posts views Thread by Sandman | last post: by
4 posts views Thread by John K Masters | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.