Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc] Mention with and except clauses in globals() #13232

Merged

Conversation

andresdelfino
Copy link
Contributor

Also changed "loop control target" to "target list", as that's the actual term used by for's grammar.

The long line is intentional. If the PR is approved I'll add the line breaks.

@JulienPalard
Copy link
Member

I just tested:

>>> bar = 42
>>> def foo():
...     global bar
...     with open("/etc/hosts") as bar:
...         ...
... 
>>> foo()
>>> bar
<_io.TextIOWrapper name='/etc/hosts' mode='r' encoding='UTF-8'>

and it works, so I'd say the proposed modification is wrong?

@andresdelfino
Copy link
Contributor Author

andresdelfino commented Oct 2, 2019

@JulienPalard I believe for/class/def behave exactly the same, that part of the documentation refers to global being written after those statements. Compare this:

def for_global_before():
    global bar
    for bar in []:
        ...

def for_global_after():
    for bar in []:
        ...
    global bar

for_global_before()
for_global_after()

# ----

def class_global_before():
    global bar
    class bar:
        ...

def class_global_after():
    class bar:
        ...
    global bar

class_global_before()
class_global_after()

# ----

def def_global_before():
    global bar
    def bar():
        ...

def def_global_after():
    def bar():
        ...
    global bar

def_global_before()
def_global_after()

In all cases, _before works, but _after fails. So does with "with" and "except".

@andresdelfino
Copy link
Contributor Author

andresdelfino commented Mar 21, 2020

@JulienPalard I have verified for/class/def/with/except behave in the same way. These are the examples I have run:

for

Doesn't fail

def for_global_before():
    global bar
    for bar in []:
        ...

Fails

def for_global_after():
    for bar in []:
        ...
    global bar

class

Doesn't fail

def class_global_before():
    global bar
    class bar:
        ...

Fails

def class_global_after():
    class bar:
        ...
    global bar

def

Doesn't fail

def def_global_before():
    global bar
    def bar():
        ...

Fails

def def_global_after():
    def bar():
        ...
    global bar

with

Doesn't fail

def with_global_before():
    global bar
    with open('/etc/hosts') as bar:
        ...

Fails

def with_global_after():
    with open('/etc/hosts') as bar:
        ...
    global bar

except

Doesn't fail

def except_global_before():
    global bar
    try:
        pass
    except Exception as bar:
        pass

Fails

def except_global_after():
    try:
        pass
    except Exception as bar:
        pass
    global bar

@andresdelfino
Copy link
Contributor Author

@JulienPalard any chance you can take a look at this? If so, please see my previous comment.

@andresdelfino andresdelfino changed the title Mention with and except clauses in globals() [doc] Mention with and except clauses in globals() Aug 21, 2020
@JulienPalard
Copy link
Member

Thanks @andresdelfino ! TIL global behave differently if placed after þ

@andresdelfino
Copy link
Contributor Author

@JulienPalard What's "þ"? My guess is that you wrote something that GitHub decided to parse :P

@andresdelfino andresdelfino deleted the mention_with_and_except_in_globals branch December 19, 2020 15:57
@JulienPalard
Copy link
Member

JulienPalard commented Dec 19, 2020

@andresdelfino it's my way to write :P

(I do it using a compose key)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip issue skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants