[Python-ideas] globals should accept parenteses for extending beyond 1 line

Steven D'Aprano steve at pearwood.info
Mon Jan 23 19:04:23 EST 2017


On Mon, Jan 23, 2017 at 09:18:54PM +0000, João Matos wrote:

> Why should I repeat global if I can use the line separation character \ 
> (like I mentioned on my 1st email) or parentheses as I suggested?

That's the wrong question.

The right question is, why should the Python language be made larger, 
more complicated, with more lines of code, to support parentheses in 
global declarations, when there are already two perfectly good 
alternatives?

    global spam
    global eggs


    global spam, \
           eggs


Even if it only adds one extra line of code to the Python interpreter, 
that's still a cost. But it will add more than that: it will require the 
implementation, tests and documentation. And all other Python 
interpretations will need to do the same: IronPython, Jython, PyPy, µPy, 
Stackless, Nuitka and possibly more. And it is a new feature that people 
have to learn.

Every new feature has a cost. Even if the cost is tiny, the question 
is, will the benefit be greater than the cost?

Supporting parentheses in from...import statements has major benefit:

    from deep.package.name import (spam,
            eggs, cheese, foo, bar, baz,
            fe, fi, fo, fum)

is a big improvement over:

    from deep.package.name import spam, eggs, cheese
    from deep.package.name import foo, bar, baz
    from deep.package.name import fe, fi, fo, fum

for at least two reasons: better efficiency, and DRY (Don't Repeat 
Yourself) with the package name.

But for globals, neither reason applies: global statements are a 
compile-time declaration, not an executable statement, so efficiency 
isn't relevant, and there is no significant DRY issue with repeating the 
keyword global itself.

So the question here is not "why shouldn't we allow parentheses?" but 
"why should we allow parentheses?"

If your answer is just "I think it looks nicer", you probably won't find 
a lot of people who agree, and even fewer people who agree enough to 
actually do the work of writing the patch, the tests and the 
documentation.

So that comes down to the most important question of all:

- are you volunteering to do the work yourself?

If there are no strong objections to adding this feature, it might be 
easier to get a core developer to offer to review your work and check it 
in, than to get a core developer to add the feature themselves.

I don't dislike this proposed feature. Nor do I like it. I would 
probably never use it: it is very rare for me to use global at all, and 
even rarer to use more than one or two globals. But if somebody else did 
the work, I wouldn't strongly object to it.


-- 
Steve


More information about the Python-ideas mailing list