object query assigned variable name?
dstanek at dstanek.com
Sat May 2 15:28:31 CEST 2009
On Fri, May 1, 2009 at 12:48 PM, Steven D'Aprano
<steve at remove-this-cybersource.com.au> wrote:
> 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
> Can't be done. Objects don't know what names they are bound to.
While objects don't know what they are assigned to, they can be made
to find out. Unless you have a good use case I don't think that you
really want to be doing it.
DecoratorTools allows you to do this. I have code that allows you to
use a a function like:
attr = inject(Customer)
The inject function does know that it is being assigned to attr. There
is tracing/frame introspection black magic involved. So much so that I
have been debating removing that feature.
The code is on Bitbucket. It has a little extra complication
because inject can also be used as a decorator. The key is the
decorate_assignment call in the inject function.
Again I don't think you really want to do this.
More information about the Python-list