The binding operator, and what gets bound to what
Chris Angelico
rosuav at gmail.com
Fri Dec 5 07:48:24 EST 2014
On Fri, Dec 5, 2014 at 11:34 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
>> I don't think this is implementation-dependent.
>
> The docs say that it is:
>
> https://docs.python.org/3/reference/simple_stmts.html#the-global-statement
>
> Names listed in a global statement MUST NOT [emphasis added] be
> defined as formal parameters or in a for loop control target,
> class definition, function definition, or import statement.
>
> CPython implementation detail: The current implementation does not
> enforce the two restrictions, but programs should not abuse this
> freedom, as future implementations may enforce them or silently
> change the meaning of the program.
Interesting, I didn't know that either.
But the first restriction _is_ enforced:
rosuav at sikorsky:~$ python3
Python 3.5.0a0 (default:23ab1197df0b, Nov 20 2014, 12:57:44)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def x(y):
... global y
...
File "<stdin>", line 2
SyntaxError: name 'y' is parameter and global
Likewise in CPython 2.7:
rosuav at sikorsky:~$ python
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def x(y):
... global y
...
File "<stdin>", line 1
SyntaxError: name 'y' is local and global
ChrisA
More information about the Python-list
mailing list