If you want X, you know where to find it (was Re: do...until wisdom needed...)
Jeff Epler
jepler at inetnebr.com
Tue Apr 24 22:15:04 EDT 2001
On 22 Apr 2001 15:52:23 -0400, Douglas Alan
<nessus at mit.edu> wrote:
> jepler at inetnebr.com (Jeff Epler) writes:
>
>> This particular example can be done with very nearly the same syntax in
>> standard Python---a dot instead of whitespace between let/set and the
>> variable in question.
>
>> >>> import letset, __main__; letset.setup(__main__)
>> >>> let.x = 3
>> >>> x
>> 3
>> >>> let.x = 4
>> Traceback (most recent call last):
>
> Thanks for the ideas. I think my coworkers might kill me if I
> programmed this way, though.
>
> How do I use this for local variables? Like so?
[snip]
I might write (all this is off the top of my head)
import letset
class NS:
def __init__(self, **kw):
self.__dict__.extend(kw)
letset.setup(self)
def add10(x):
l = NS(locals())
l.let.y = 10
return l.x + l.y
which eliminates one line of boilerplate, and gets the arguments into `l'.
You could also do
def ns(dict):
x = NS(dict)
return x.let, x.set, x
def add10(x):
let, set, get = ns(locals())
let.y = 10
return get.x + get.y
A package like bytecodehacks plus custom importhooks might permit you
to automatically insert the line of boilerplate.
By the way, this is also a great way to get static function variables:
def add10(x, static=NS(y=10)):
return x + static.y
Jeff Epler
jepler at inetnebr.com
More information about the Python-list
mailing list