Python 2.4: Why only assignments to None are forbiden?

Bengt Richter bokr at oz.net
Fri Nov 12 20:08:22 EST 2004


On Fri, 12 Nov 2004 13:08:30 -0800, James Stroud <jstroud at mbi.ucla.edu> wrote:

>
>Forbidding assignment to arbitrary things seems like a bad idea.
>
>I remember some learning manual recommended this (or the equivalent):
>
Two things wrong in the following, as noted:
>def do_stuff_to_bob(bob=None):
#XXX# >  if not bob:
   if bob is None:  # this is the usual gerneral idiom, so that e.g., one could
                    # distinguish from a caller passing a legitimate empty list []
                    # (though of course you could use if not bob if that's what you meant ;-)
>    bob = ["carol","ted","alice"]
#    ^^^^^ -- this does not assign to None, it rebinds bob,
#             which has no effect on the previously bound object (in this case None)
#             That's a serious misunderstanding of python's assignment semantics, which
#             your momentary lapse should not be allowed to promote to newbies ;-)

>  #do stuff to bob
>  return bob
>
>otherwise bob would already be defined from the last call (because it is a 
>mutable type? I can't remember.). How else to handle this in 2.4?
>
>On Friday 12 November 2004 01:11 pm, Josef Meile wrote:
>
>> Hi,
>>
>> Textually from the highlights of python 2.4:
>>
>> "Assigning to None - the compiler now treats assigning to None as a
>> SyntaxError."
>>
>> I think in general assignments to built-in types, functions, and
>> variables should be also forbiden.
>
>
>James

 Python 2.4b1 (#56, Nov  3 2004, 01:47:27)
 [GCC 3.2.3 (mingw special 20030504-1)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> None = 'foo'
 SyntaxError: assignment to None
 >>> 1234 = 'foo'
 SyntaxError: can't assign to literal
 >>> bob = None
 >>> bob
 >>> repr(bob)
 'None'
 >>> bob = 1234
 >>> bob
 1234

Regards,
Bengt Richter



More information about the Python-list mailing list