Intercepting binding?

Carl Banks pavlovevidence at gmail.com
Thu Sep 24 07:33:20 EDT 2009


On Sep 23, 7:41 pm, andrew cooke <and... at acooke.org> wrote:
> On Sep 23, 10:11 pm, Dave Angel <da... at ieee.org> wrote:
>
> > This comes up periodically in this list, and the answer is always
> > something like:  you can't get there from here.
>
> Well, I'm both flexible and desperate, so this is a possible route
> (perhaps near enough):
>
> import sys
>
> class Foo(object):
>
>     def __rlshift__(self, name):
>         try:
>             raise Exception()
>         except:
>             locals = sys.exc_traceback.tb_frame.f_back.f_locals
>             locals[name] = self
>
> if __name__ == '__main__':
>     foo = Foo()
>     'a' << foo
>     print(a)

Did you try this inside a function?   (Hint: it won't work in a
function.)

BTW, if you are desperate to do this then I'd say you lack good
perspective.  You are subverting some of the most basic behavior of
Python here for something of marginal and only occasional usefulness.
If you are that desperate just retype the name, it won't be the end of
the world.

a = Foo('a')


Carl Banks



More information about the Python-list mailing list