Python handles globals badly.

Chris Angelico rosuav at
Sat Sep 5 02:27:05 CEST 2015

On Sat, Sep 5, 2015 at 5:11 AM,  <tdev at> wrote:
> 1. optional keyword "global" (if technical possible)

As we've been saying in this thread, the problem isn't the
technicalities of implementation, but the ambiguity of syntax. To
eliminate the global statement, you need to either (a) declare all
your globals at module level, the way C-like languages do, or (b)
adorn every usage with either a magic naming convention or a

If you want the first one, well, there are languages like that, and
you're welcome to use those. For the latter, it's easy enough to do
something like this:

import types
_g = types.SimpleNamespace()

def accumulate(x):
    _g.accum += x
    return _g.accum

Look, Ma! No global statement!

> 2. switch statement

Check out the suggestions in PEP 3103. There are some plausible
options, and the known problems with them.

> 3. less restrictive indentation
>    (as if it would really matter forgetting an empty space somewhere
>     if something is intended more right than is the context clear for me)

In the face of ambiguity, refuse the temptation to guess. Can you
imagine the horrors if Python started guessing at indentation?

count = 0
for file in files:
    for line in open(file):
    count += 1

It's obvious that process(line) goes inside the nested loop. What
about the counter? Literally the only difference between "count the
lines processed" and "count the files processed" is the indentation of
one line. You could make these kinds of things unambiguous, but only
by adding some other rule, like "always have a blank line at the end
of a loop", which would be just as restrictive.

> 4. universal scope

You can inject stuff into the built-ins, is that good enough?

> 5. goto label

Already exists!

> 6- "include" script statement (extending namespace to another script, like PHP)

Ugh. There are a number of ways you can describe the semantics of
"include", and you picked what is, in my opinion, the very worst.
PHP's include operation switches back to HTML mode, which isn't itself
unreasonable, but it highlights the fact that "starting in HTML mode"
is stupid for any system of the size and complexity to want include();
but notably, it is *not* a simple text inclusion. You can include at
top level, and that works fine (I think). You can include inside a
function, and that mostly works (but it's not the same as dumping the
source code in at that point). But you can't, for some bizarre reason,
include inside a class definition. Not at all.

Fortunately, it's easy enough to preprocess source code. And if you
want that functionality in Python, that's probably the best way to do
it, too. The only special I can think of would be to indent the file
to the exact level that the #include directive is indented. (And yes,
I think using "#include" in Python would be correct; that way, syntax
highlighters read it as a comment.)

Most of what you want can be done already. If you want the language to
grow these features, you need to explain how the feature is different
from what can already be done.


More information about the Python-list mailing list