Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95935

Re: Python handles globals badly.

References <c3363$547e74fe$5419aafe$24179@news.ziggo.nl> <3bbeafa4-d756-46f8-9750-2fca29617cf4@googlegroups.com>
Date 2015-09-04 00:03 +1000
Subject Re: Python handles globals badly.
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.67.1441289040.8327.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Sep 3, 2015 at 11:22 PM,  <tdev@freenet.de> wrote:
> Sample "Good":
> module A
>    _x = 0
>
>    def y():
>         _x=1
>
>
> why - this I have tried and try to explain in my and your posts
>       in the hope a PEP will arise which frees me and hopefully
>       a lot other developers getting forced to use "global"
>       (If developers need this "global" - ok, but I and
>         hopefully more want not to be forced with that
>         code-contaminator, especially having a lot more vars)

Okay. Let's suppose that some magic is worked out that makes this
work. Now let's try this example:

def x(q):
    for word in generate_words():
        if word.matches(q):
            return word

def y():
    word = x("blue")
    otherword = x("green")
    if word < otherword: return otherword
    return x("red")

How would you reason about this code? Would you not expect that the
instances of 'word' in each function are completely independent? (And
while this is a contrived example, the exact same thing happens *a
lot*, where the name used in a "return" statement is the same as the
name that thing gets assigned to. After all, if it's a logical name
for that thing in one place, it's likely a logical name in the other,
too.) According to your proposal, they would cease to be independent
if the module grows an attribute 'word'. Since, in Python, such
attributes can be injected from outside, there is literally no way to
reason about this code in isolation. That makes it very difficult to
track down problems.

Definitely do not like this.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Python handles globals badly. tdev@freenet.de - 2015-09-03 06:22 -0700
  Re: Python handles globals badly. Chris Angelico <rosuav@gmail.com> - 2015-09-04 00:03 +1000
  Re: Python handles globals badly. Michael Torrie <torriem@gmail.com> - 2015-09-03 09:50 -0600
  Re: Python handles globals badly. Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-03 10:15 -0600
  Re: Python handles globals badly. Michael Torrie <torriem@gmail.com> - 2015-09-03 10:43 -0600
    Re: Python handles globals badly. Steven D'Aprano <steve@pearwood.info> - 2015-09-04 12:05 +1000
    Re: Python handles globals badly. pepekbabi5@gmail.com - 2015-09-06 15:35 -0700
  Re: Python handles globals badly. MRAB <python@mrabarnett.plus.com> - 2015-09-03 19:29 +0100
  Re: Python handles globals badly. Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-03 18:41 -0400

csiph-web