PEP 285: Adding a bool type

Bengt Richter bokr at oz.net
Mon Apr 8 12:03:32 EDT 2002


On Mon, 08 Apr 2002 10:37:24 GMT, Alex Martelli <aleax at aleax.it> wrote:

>Bengt Richter wrote:
>
>> On 7 Apr 2002 13:43:29 -0400, aahz at pythoncraft.com (Aahz) wrote:
>> [...]
>>>
>>>Assignments in expressions will never happen.
>>>
>> I guess you mean they won't be spelled concisely ;-)
>> 
>>  >>> a='eigh'
>>  >>> a
>>  'eigh'
>>  >>> locals().__setitem__('a','bee') and 0 or a
>>  'bee'
>
>Now try that same, identical expression inside a 
>function, will you...?
>
Once is not never. Aahz just pushed my never-say-never button,
and I found one expression that worked ;-)

But why "identical" ;-) If I can find another spelling that works
everywhere (and it wouldn't surprise me if you had one in mind ;-)
wouldn't that be interesting?

How about globals() instead of locals()? (Not that that is exactly
the same. The implied goal was an expression that would have the
effect of the statement a=b' as a side effect when evaluated
in the same context, while having an expression value of b,
analogous to the way it works in C).

>You can't assume locals() is modifiable at all:
>attempts to modify it may fail silently and/or
>they may raise exceptions ("undefined behavior").
>
It did _seem_ to work (and did, at least well enough, if you're
in a hurry to refute "never" ;-), and you do have to look for
the documentation of restrictions:

 >>> help(locals)
 Help on built-in function locals:

 locals(...)
     locals() -> dictionary

     Return the dictionary containing the current scope's local variables.

Do you think that should be modified to say it doesn't necessarily work
like an ordinary dictionary, as is mentioned in a footnote of
Python22/Doc/ref/execframes.html? :

"""
The current implementations return the dictionary actually used to implement
the namespace, except for functions, where the optimizer may cause the local
namespace to be implemented differently, and locals() returns a read-only dictionary.
"""

Regards,
Bengt Richter



More information about the Python-list mailing list