classification
Title: strange 'global'
Type: behavior
Components: Interpreter Core Versions: Python 2.5, Python 2.4
process
Status: closed Resolution: invalid
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, oyster
Priority: Keywords:

Created on 2008-01-22 03:12 by oyster, last changed 2008-01-22 03:24 by gvanrossum.

Messages
msg61482 (view) Author: Lee June (oyster) Date: 2008-01-22 03:12
hi, all. my friend found a strange behave with 'global', I posted it
in the 'python-list@python.org' as this:
[quote]
why the following 2 prg give different results? a.py is ok, but b.py
is 'undefiend a'
I am using Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC
v.1310 32 bit (Intel)] on win32
#a.py
def run():
    if 1==2:                # note, it always False
        global a
    a=1

run()
a

#b.py
def run():
      a=1

run()
a
[/quote]

I believe that is bug which breaks the consistency of python syntax and
common english langauge sense- just like we can do '1>None' in 
python2.x,
but not in python 3.x.
So please fix this bug.
If you do think that is the feature of python and refuse to fix it, 
please
at least mention the craziness in the document. Uhm, maybe I can do the 
later,
but I don't think it is good.


btw, there are some replies(you can search them in the archive), I 
think the
other users met more questions, for example

From: "Gabriel Genellina" <gagsl-py2@yahoo.com.ar>
To: python-list@python.org
Date: Mon, 21 Jan 2008 18:36:19 -0200
Subject: Re: problem with 'global'
En Mon, 21 Jan 2008 17:36:29 -0200, Duncan Booth
<duncan.booth@invalid.invalid> escribi?:

> Marc 'BlackJack' Rintsch <bj_666@gmx.net> wrote:
>
>> On Mon, 21 Jan 2008 17:08:46 -0200, Gabriel Genellina wrote:
>>
>>> The future statement is another example, even worse:
>>>
>>> if 0:
>>>      from __future__ import with_statement
>>>
>>> with open("xxx") as f:
>>>      print f
>>
>> In Python >=2.5 it's a compile time error if that import is not the 
very
>> first statement in a source file.
>>
> That doesn't appear to be the case. With Python 2.5.1 the example
> Gabriel quoted will compile and run.

Yes, but now I've noticed that the 0 has some magic. The code above 
works
with 0, 0.0, 0j and ''. Using None, False, () or [] as the condition, 
will
trigger the (expected) syntax error.

Mmm, it may be the peephole optimizer, eliminating the dead branch. This
function has an empty body:

def f():
  if 0: print "0"
  if 0.0: print "0.0"
  if 0j: print "0j"
  if '': print "empty"

py> dis.dis(f)
  5           0 LOAD_CONST               0 (None)
              3 RETURN_VALUE

The other false values tested (False, None, () and []) aren't optimized
out.

--
Gabriel Genellina
msg61483 (view) Author: Guido van Rossum (gvanrossum) Date: 2008-01-22 03:24
This isn't going to change. 'global' is one of those rare pieces of
syntax that are not executed by the interpreter but detected by the parser.

Note that there are other things like this, e.g. the presence of 'yield'
in a function makes it a generator.

FWIW, the "if 0:" issue is different; that's a genuine bug, see issue #1875.
History
Date User Action Args
2008-01-22 03:24:27gvanrossumsetstatus: open -> closed
resolution: invalid
messages: + msg61483
nosy: + gvanrossum
2008-01-22 03:12:42oystercreate