Why no warnings when re-assigning builtin names?

Gerrat Rickert grickert at coldstorage.com
Mon Aug 15 17:52:05 EDT 2011


With surprising regularity, I see program postings (eg. on
StackOverflow) from inexperienced Python users  accidentally
re-assigning built-in names.

 

For example, they'll innocently call some variable, "list", and assign a
list of items to it.

...and if they're _unlucky_ enough, their program may actually work
(encouraging them to re-use this name in other programs).

 

If they try to use an actual keyword, both the interpreter and compiler
are helpful enough to give them a syntax error, but I think the builtins
should be "pseudo-reserved", and a user should explicitly have to do
something *extra* to not receive a warning.

I'd suggest: "from __future__ import allow_reassigning_builtins", but I
think this abuse of the __future__ module likely isn't welcome.

 

I know that for testing purposes, this functionality is very convenient,
and I'm not suggesting it be removed.

In these cases, it would be trivial to just require something explicit,
telling the interpreter that the programmer was aware they were
assigning to a builtin name.

 

The situation is slightly different for modules that come with Python.  

Most of us would cringe when seeing something like:  `string = "Some
string"`;  but at least the user has to explicitly import the string
module for this to actually cause issues (other than readability).

 

 

What sayest the Python community about having an explicit warning
against such un-pythonic behaviour (re-assigning builtin names)?

 

Regards,

    Gerrat

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110815/e3f5fa4d/attachment.html>


More information about the Python-list mailing list