Instantly share code, notes, and snippets.

Embed
What would you like to do?

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!

@lonewolf28

This comment has been minimized.

lonewolf28 commented Sep 21, 2015

Very useful. Thank you.

@azide0x37

This comment has been minimized.

azide0x37 commented Oct 4, 2015

This is wonderful, thanks for documenting this!

@frodon1

This comment has been minimized.

frodon1 commented Oct 12, 2015

Hello !

Do you know if there was a reason this following code was not proposed ?

def find(seq, target):
    for i, value in enumerate(seq):
        if value == target:
            return i
    return -1

instead of

def find(seq, target):
    for i, value in enumerate(seq):
        if value == target:
            break
    else:
        return -1
    return i
@mongoose11235813

This comment has been minimized.

mongoose11235813 commented Dec 10, 2015

@frodon1 Raymond Hettinger actually mentioned that this example can be simplified using an early return. See video. He said that for...else can be useful when a situation like this occurs "intermeshed with other more complex code in other operation so there is not a shortcut out".

@mongoose11235813

This comment has been minimized.

mongoose11235813 commented Dec 11, 2015

I added notes for python 3 users. Could you please pull this changes? https://gist.github.com/mongoose11235813/151a04156e1a709ad6e4

@JeffPaine

This comment has been minimized.

Owner

JeffPaine commented Dec 28, 2015

@mongoose11235813 Thanks! You're changes have been merged. Sorry for the delay. Some searching turned up that github doesn't have notifications for comments on gists :(

For my (or other's) future reference on how I merged in the changes from your fork (since gists don't support pull requests):

 git remote add mongoose11235813 git@gist.github.com:151a04156e1a709ad6e4.git
 git fetch mongoose11235813 
 git merge mongoose11235813/master 
 git push origin master
@greut

This comment has been minimized.

greut commented Mar 11, 2016

There is yet a better way of counting things: https://treyhunner.com/2015/11/counting-things-in-python/

@robinchew

This comment has been minimized.

robinchew commented May 18, 2016

Hi, under "Using decorators to factor-out administrative logic" can you @JeffPaine please fix the if statement?

@saqib-nadeem

This comment has been minimized.

saqib-nadeem commented May 31, 2016

Cool!

@DimitriPapadopoulos

This comment has been minimized.

DimitriPapadopoulos commented Jun 5, 2016

Nice to see this written. Please change dicitonary keys to dictionary keys.

@yxliang01

This comment has been minimized.

yxliang01 commented Jun 9, 2016

It's so useful! Thanks

@JeffPaine

This comment has been minimized.

Owner

JeffPaine commented Jun 27, 2016

@robinchew: done, thanks! 😃

@DimitriPapadopoulos: Done, thanks! 🍰

All: glad everyone is enjoying and learning from this write up. Raymond Hettinger really gave a wonderful talk here!

@sebasibarguen

This comment has been minimized.

sebasibarguen commented Jun 30, 2016

Thanks for the notes @JeffPaine!

@jhadjar

This comment has been minimized.

jhadjar commented Sep 24, 2016

That was a great talk. The ratio content/time was huge. There's someone in the video comments (DarthChrisB) who time-stamped the video with each topic.

Thanks for sharing.

@SF-Zhou

This comment has been minimized.

SF-Zhou commented Oct 10, 2016

Interesting~

@ZuZuD

This comment has been minimized.

ZuZuD commented Dec 4, 2016

You should precise the library for defaultdict, influence, deque... Good job thank you !

@satbirsoni

This comment has been minimized.

satbirsoni commented Jan 4, 2017

Thanks for sharing.

@pybites

This comment has been minimized.

pybites commented Jan 10, 2017

this is awesome, thanks!

@ikem-krueger

This comment has been minimized.

ikem-krueger commented Mar 16, 2017

Typo on "Factor-out temporary contexts":

yield fieldobj

@AurielleP

This comment has been minimized.

AurielleP commented Mar 24, 2017

Thanks so much for writing this up! I actually had just started to do the same, but then decided I should at least check online to see if anyone else already had!

@michaelbasca

This comment has been minimized.

michaelbasca commented Apr 9, 2017

I see @ikem-krueger mentioned this but you have a typo with the redirect_stdout function. It should yield fileobj instead of 'fieldobj'.

You can fork my fix below:

https://gist.github.com/michaelbasca/0186c7c16326d76fa1764a8d09d6854b

@jellyjellyrobot

This comment has been minimized.

jellyjellyrobot commented May 16, 2017

cool!

@cindylyl

This comment has been minimized.

cindylyl commented Jun 12, 2017

Thanks for sharing. This really useful!

@BartoszCki

This comment has been minimized.

BartoszCki commented Jul 20, 2017

Hi!
This part is not working (even indentation fixed)
https://gist.github.com/JeffPaine/6213790#better-9
Consider updating from my fork:
https://gist.github.com/BartoszCki/fd918625972e6637dc41e59d0d822db6

@zenware

This comment has been minimized.

zenware commented Aug 7, 2017

# First we are shown this as the better way to reverse,
reversed(colors)

# And then this is shown for reversing sorted elements
sorted(colors, reversed=True)

# Why not this?
reversed(sorted(colors))

"""
I'm sure there's a perfectly good reason, like, "There's an algorithm with which sorting and reversing 
can be done together, faster than they can be done apart.", I just think that it should be made explicitly apparent.
"""
@diegorodriguezv

This comment has been minimized.

diegorodriguezv commented Aug 28, 2017

Hey. Small typo: explicidly -> explicitly.
Thank you very much.

@Roychenlei

This comment has been minimized.

Roychenlei commented Sep 16, 2017

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

@KaijianYou

This comment has been minimized.

KaijianYou 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

This comment has been minimized.

Owner

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