Why no warnings when re-assigning builtin names?

rantingrick rantingrick at gmail.com
Mon Aug 15 21:12:53 EDT 2011


On Aug 15, 5:13 pm, Philip Semanchuk <phi... at semanchuk.com> wrote:
> On Aug 15, 2011, at 5:52 PM, Gerrat Rickert wrote:
>
> > 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).
>
> Or they'll assign a class instance to 'object', only to cause weird errors later when they use it as a base class.
>
> I agree that this is a problem. The folks on my project who are new-ish to Python overwrite builtins fairly often.

Simple syntax hilighting can head off these issues with great ease.
Heck, python even has a keyword module, and you get a list of built-
ins from the dir() function!

import keyword
import __builtin__
PY_BUILTINS = [str(name) for name in dir(__builtin__) if not
name.startswith('_')]
PY_KEYWORDS = keyword.kwlist

Also Python ships with IDLE (which is a simplistic IDE) and although i
find it needs a bit of work to be what GvR initially dreamed, it works
good enough to get you by. I always say, you must use the correct tool
for the job, and syntax hilight is a "must have" to avoid these
accidents.



More information about the Python-list mailing list