Why no warnings when re-assigning builtin names?

rantingrick rantingrick at gmail.com
Tue Aug 16 11:44:28 EDT 2011


On Aug 16, 9:13 am, Philip Semanchuk <phi... at semanchuk.com> wrote:

> "Sometimes X is safe and sometimes it isn't" can be said
> of many, many things, from taking a walk down the street
> to juggling with knives. But it has little to do with
> whether or not Python should issue a warning in the
> specific case we're talking about.

I think any Python n00b should be writing code in n editor that is
"safe for beginners" AND is also teaching tool. IDLE is probably the
best thing a new Python programmer could use. It has syntax hilight,
smart indention, call tips, code completion, a class browser, greping
tools, and more! I do admit the tool is lacking in many areas
(including code base) that would be atrocious to an experienced
Pythonista, however, the tool is perfect for n00bs (and myself and
other pythoinistas use it all the time!)

> There's a lot of ground to cover between "newcomer who has
> learned about a particular warning" and "coder who
> regularly shadows builtins on purpose".

One word: SYNTAX HILIGHT

> I have never shadowed a builtin deliberately. I've done it
> accidentally plenty of times. There are 84 builtins in my
> version of Python and I don't have them all memorized. The
> fact that my editor colors them differently is the only
> thing I have to back up my leaky memory. Not all editors
> are so gracious.

So you have syntax hilight however you still shadow builtins?  Oh
dear, this problem is worse than i initially suspected!

> You can coerce any example to apply to an argument for or
> against such a warning, but I think the general case is
> that Python could reduce unintended consequences by
> warning when vars erase builtins.  (<=== How many builtins
> did I use in that sentence?)

Well let's see:

py> import keyword
py> import __builtin__
py> PY_KWS = keyword.kwlist
py> PY_BLT = dir(globals()['__builtins__'])
py> s = """\
You can coerce any example to apply to an argument for or against such
a warning, but I think the general case is that Python could reduce
unintended consequences by warning when vars erase builtins. """
py> fmtstr = '{0} -> {1}'
py> for word in s.split(' '):
...     if word in PY_BLT:
...         print fmtstr.format('builtin', word)
...     elif word in PY_KWS:
...         print fmtstr.format('KeyWord', word)
...
builtin -> coerce
builtin -> any
builtin -> apply
KeyWord -> for
KeyWord -> or
KeyWord -> is
builtin -> reduce
builtin -> vars



More information about the Python-list mailing list