Binding a reference to a variable (solved?!?)
Michael Hudson
mwh at python.net
Thu Apr 11 09:55:28 EDT 2002
Patrick Miller <patmiller at llnl.gov> writes:
> Here's a scary way to do what Andrew was looking for. It has
> the proper form and fewer (but still significant) restrictions.
>
> >>> x = 7
> >>> set(x,99)
> >>> print x
>
> I'll likely submit the following as a ASPN recipe, though
> it clearly needs a better description of motivation. Just
> because you CAN do a thing doesn't mean you SHOULD
> do a thing.
Do you know about my bytecodehacks? I think you might like
them... (though they only really work with 1.5.2).
[...]
> x = 3
> y = [1,2,3,4,]
> z = {"something": 0}
>
>
> def set(lhs,rhs,G=globals()):
> address = id(lhs)
This is a pretty bad way of identifying the passed in value. What you
"want" to do is look at
sys._getframe().f_back.{f_lasti,f_code.co_code}, poke around until you
find the
LOAD_FAST 'x'
instruction or whatever, and go from there.
Of course, this is all absurdly hacky, but kind of fun. It should
come with a kind of "explosive licence" -- if you use it in production
code, your shoes explode.
Cheers,
M.
--
Programming languages should be designed not by piling feature on
top of feature, but by removing the weaknesses and restrictions
that make the additional features appear necessary.
-- Revised(5) Report on the Algorithmic Language Scheme
More information about the Python-list
mailing list