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

Defer #5

Open
tovare opened this issue Sep 17, 2018 · 2 comments
Open

Defer #5

tovare opened this issue Sep 17, 2018 · 2 comments

Comments

@tovare
Copy link

tovare commented Sep 17, 2018

In Python it would be useful to list the

with open('workfile') as f:
read_data = f.read()
f.closed

In addition to or instead of the try except.
https://docs.python.org/3/tutorial/inputoutput.html

@peterbe
Copy link
Owner

peterbe commented Sep 17, 2018

I agree! I don't do anything like that with open in Python.

It would probably be better to do something entirely different with the Python example, in the finally: block.

@riggsd
Copy link

riggsd commented Oct 14, 2018

Agreed. The closest equivalent to Go's defer is not a try/except, but the context manager (aka with statement). See: https://www.python.org/dev/peps/pep-0343/

Of course, the closest thing to Python's try/except that we're supposed to use in Go is the defer statement. You already touch on Go's panic/recover, which is closer in spirit to Python's try/except, though the defer statement is intended as more general purpose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants