Python 2.4: Why only assignments to None are forbiden?

Josef Meile jmeile at hotmail.com
Fri Nov 12 16:11:59 EST 2004


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. It's a common mistake to do things 
like this:

 >>> def getFileName(file):
...   parts=file.split('/')
...   return parts('/')[-1]

Specially if you come from python 2.1.x where "file" didn't exist. 
Instead, there was "open"

On the example's context, file is a string and won't cause any damage 
because it is inside a function, so, the scope is local and it will be 
deleted after the function call. But think what would happen if somebody 
defines "file" as a global variable and other people use that code? For 
the author won't be any consequences at all, because if s/he does this, 
it means that probably s/he isn't working with files, but if somebody 
else takes that code and doesn't see this, then s/he will invest some 
time trying to find the bug.

Doing this validation for python 2.4.x will break some things, like the 
validation with "None" does. But I think it's better when you now that 
you are trying to use a "reserved word", you could as well use the "str" 
as an example, which I think is also common on newies.

Regards,
Josef



More information about the Python-list mailing list