Printable string for 'self'

D

Don Taylor

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.
 
B

Ben Cartwright

Don said:
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.
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)
print 'about to call meth2(%r, %r)' % (x, y)
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:
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
function call from parent scope:
x = f(3, fred)


But defining __repr__ is far easier and more common.

--Ben
 
M

Michael Tobis

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
 
D

Duncan Booth

Michael said:
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.
print x


What were you planning to do with this object exactly that didn't involve
binding it to any other names during its lifetime?
 
M

Michael Tobis

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,290
Messages
2,571,452
Members
48,129
Latest member
DianneCarn

Latest Threads

Top