Favorite non-python language trick?
Bengt Richter
bokr at oz.net
Sun Jun 26 18:15:37 EDT 2005
On Sun, 26 Jun 2005 14:30:15 +1000, Steven D'Aprano <steve at REMOVETHIScyber.com.au> wrote:
>On Sat, 25 Jun 2005 23:08:10 +0000, Bengt Richter wrote:
>
[...]
>>
>> The single line replacing
>> """
>> with colour do begin
>> red := 0; blue := 255; green := 0;
>> end;
>> """
>> follows:
>> >>> vars(color).update(red=0, blue=255, green=0)
>
>
>The point is that a hypothetical "with" block would have to allow
>arbitrary access to dotted names: getting, setting, deletion, and method
>calling, not just one very limited form of keyword assignment.
Point taken.
>
>I understand how to manipulate __dict__ as an more complicated (dare I say
>obfuscated?) way of assigning to object attributes.
>
Yes, it's a bit obfuscated.
>
>[snip]
>
>> We can clear those attributes from the instance dict:
>>>>> vars(colour).clear()
>>>>> vars(colour)
>> {}
>
>Which has the unfortunate side effect of also destroying any other
>instance attributes.
You wouldn't do something unfortunate, would you? ;-)
I just wanted to clear them all at that point in the interaction.
>
>
>>>you might do this:
>>>
>>>with myobject:
>>> # read a class attribute
>>> print .__class__.myattribute
>>> # set an instance attribute
>>> .widget.datapoints[.collector] = .dispatcher(.widget.current_value)
>>>
>>
>> def mywith(o=myobject):
>> # read a class attribute
>> print o.__class__.myattribute
>> # set an instance attribute
>> o.widget.datapoints[o.collector] = o.dispatcher(o.widget.current_value)
>> mywith()
>
>[snip]
>
>> Is a one-character prefix to the dot objectionable?
>
>That's a good workaround, subject to namespace pollution issues, and one
>that I'm aware of. Although do you really want to be creating a unique
>function definition every time you want to use the with idiom?
The only thing, ISTM, that would not also be unique in the with construct
is the function name, and you snipped the anonymnous def version that would
eliminate that. The main question in my mind would be binding of non-leading-dot
names. Would they work as in the suite of an "if" (i.e., binding in the same scope)
or as in a function with a new local scope that disappears on exit?
A function solves the alias name pollution problem, but prevents rebinding
anything external except explicitly declared globals, and closure cell vars
can't be rebound currently.
>
>I'm not about to stop using Python because of the lack of Pascal-like
>"with" blocks. It is a nice feature to have, but I'm aware that Guido
>prefers explicit to implicit and "with" is extremely implicit.
>
I wonder what happens when you have multiple withs, e.g.,
with obj_a:
.x = 1
with obj_b:
.x = 2
.y = 3
(I guess you would have to have a stack with only the latest with effective).
whereas a simple aliasing syntax like
(a=obj_a, b=obj_b):
a.x = 1
b.x = 2
b.y = 3
is unambigous and concise and the scope of the namescan be limited
to prevent name pollution at compile time.
OTOH, what should
(a=obj_a):
a = 123 # XXX ??
mean? Should it mean obj_a=123 and rebind in that scope,
like a macro substitution of names at compile time?
I guess the corresponding with thing would be
with obj_a:
. = 123
Regards,
Bengt Richter
More information about the Python-list
mailing list