object query assigned variable name?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri May 1 12:48:32 EDT 2009

On Fri, 01 May 2009 09:24:10 -0700, warpcat wrote:

> I'd like it to print, when instanced, something like this:
>>>> s = Spam()
> I’m assigned to s!
> But it seems prohibitively hard (based on my web and forum searches) for
> an object to know what variable name is has been assigned to when
> created.

Can't be done. Objects don't know what names they are bound to.

Furthermore, your question is based on an invalid assumption. Objects 
aren't necessarily bound to a single name, they can be bound to zero, 
one, or more names.

What name should Spam print in these examples?

alist = [1, 2, Spam(), 4]
x = y = z = Spam()


More information about the Python-list mailing list