Skip to content

Instantly share code, notes, and snippets.

@JeffPaine
Last active January 14, 2023 05:37
Show Gist options
  • Save JeffPaine/6213790 to your computer and use it in GitHub Desktop.
Save JeffPaine/6213790 to your computer and use it in GitHub Desktop.

Moved

Now located at https://github.com/JeffPaine/beautiful_idiomatic_python.

Why it was moved

Github gists don't support Pull Requests or any notifications, which made it impossible for me to maintain this (surprisingly popular) gist with fixes, respond to comments and so on. In the interest of maintaining the quality of this resource for others, I've moved it to a proper repo. Cheers!

@Roychenlei
Copy link

List Comprehensions and Generator Expressions

result = []
for i in range(10):
s = i ** 2
    result.append(s)
print sum(result)

IndentationError: expected an indented block

@revado
Copy link

revado commented Oct 9, 2017

Factor-out temporary contexts

@contextmanager
def redirect_stdout(fileobj):
    oldstdout = sys.stdout
    sys.stdout = fileobj
    try:
        yield fieldobj    # 'fieldobj' may be 'fileobj'?
    finally:
        sys.stdout = oldstdout

@JeffPaine
Copy link
Author

JeffPaine commented Dec 3, 2017

I've addressed all comments here and moved this gist to a proper repo: https://github.com/JeffPaine/beautiful_idiomatic_python (so Pull Requests can be received and I get notifications, which isn't possible for gists).

If you have any other fixes / comments, please make them on that repo as I won't be checking back here again in the future. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment