object query assigned variable name?

AKEric warpcat at sbcglobal.net
Fri May 1 13:16:26 EDT 2009

On May 1, 10:03 am, David Robinow <drobi... at gmail.com> wrote:
> On Fri, May 1, 2009 at 12:24 PM, warpcat <warp... at sbcglobal.net> wrote:
> > I've passed this around some other groups, and I'm being told
> > "probably not possible".  But I thought I'd try here as well :)   I
> > *did* search first, and found several similar threads, but they
> > quickly tangented into other specifics of the language that were a bit
> > over my head :)  At any rate, here's a simple example, I'd love to
> > know if as shown, is somehow possible:
> > Given an object:
> > class Spam(object):
> >    def __init__(self):
> >        # stuff....
> > 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.  Querying 'self' in __init__ returns a memory location, not
> > the variable name passed in.
> > If you're wondering why I'm trying to figure this out, this is just
> > part of my continued learning of the language and pushing the bounds,
> > to see what is possible ;)
> > Any thoughts?
> > --
> >http://mail.python.org/mailman/listinfo/python-list
> Others have explained to you that this is not possible.
> I'll just point out that your method for learning the language is not optimal.
> If you had gotten a recipe to do what you asked, how would it help you
> write better programs?
> I suggest that you start programming. If you get stumped, feel free to ask here.


No one has yet told me it isn't possible (until other posts in this
thread), they just alluded to it.  Common replies are "well, maybe
this way, or maybe that way".  I myself help on other language forums
(that I know far better than Python), and sometimes it's helpful for
someone to just say "no, you can't do it" (if you truly know you
can't), which is what I was looking for here.

But to argue the way I'm learning the language isn't optimal is a bit
short-sited, based on a single-post query.  It's valuable to both know
what you can, can't, should and shouldn't do, and one shouldn't be
debased for asking such questions.

More information about the Python-list mailing list