Transforming Code into Beautiful, Idiomatic Python

Sdílet
Vložit
  • čas přidán 9. 06. 2024
  • Raymond Hettinger
    Learn to take better advantage of Python's best features and improve existing code through a series of code transformations, "When you see this, do that instead."

Komentáře • 659

  • @rafehqazi8539
    @rafehqazi8539 Před 8 lety +1464

    Note to Python 3 Users:
    - izip is just zip
    - d.iteritems() is just d.items()
    - To use defaultdict: You have to do the following --> from collections import defaultdict

    • @suburbanmoscow9067
      @suburbanmoscow9067 Před 7 lety +12

      Rafeh Qazi thx for the additional info

    • @CleverProgrammer
      @CleverProgrammer Před 7 lety +19

      Woah, did not notice my comment was sitting at 65 likes haha. I now have my own computer programming CZcams channel! Thanks for commenting Suburban and making me see this!

    • @suburbanmoscow9067
      @suburbanmoscow9067 Před 7 lety +5

      You are welcome. I will check out your channel as well! Cheers

    • @javierRC82857
      @javierRC82857 Před 7 lety

      Rafeh Qazi but in Python 2.7 zip is an iterator? izip,zip or range,xrange why not use iterator for everything and let the programmer choose if he want expand the list?

    • @nmertsch8725
      @nmertsch8725 Před 7 lety +6

      Javier I'm not sure if this is what you asked for, but you can use "list(range(x))" or "list(zip(x,y))", if you really want to have a list instead of an iterable object.

  • @juliushinze5177
    @juliushinze5177 Před 7 lety +163

    10:20 Beautiful syntax highlighting

    • @jonty3551
      @jonty3551 Před 4 lety

      Lol

    • @nickbarss1693
      @nickbarss1693 Před 4 lety +5

      I remember my prof for C++ and OOP had every other word/letter in his notes/code highlighted with no common theme whatsoever.

  • @jeffkirchoff14
    @jeffkirchoff14 Před 2 lety +6

    Man YT recommended it after 8 yrs
    In search of gold I found diamond

  • @qg4091
    @qg4091 Před 7 lety +541

    This guy is seriously amazing, love his style of teaching

    • @johncherry108
      @johncherry108 Před 5 lety +23

      Pity his ego needs to be continuously stroked with applause.

    • @Pulsar77
      @Pulsar77 Před 5 lety +43

      John Cherry Oh FFS, it's a joke. First rule of becoming a Python programmer: have a sense of humor.

    • @heinzguderian9980
      @heinzguderian9980 Před 5 lety +10

      He reminds me a bit too much of a pastor for me to be completely comfortable, (like from a Pentecostal church or something). Or an unctuous salesman telling us about his schemes. Material was great, though.

    • @DimitrisK5
      @DimitrisK5 Před 4 lety +13

      @@johncherry108 It's one of his techniques to keep the audience engaged. He is an excellent speaker and he obviously does that on purpose. You might also notice that he asks the audience all the time if they learnt something new, or how many knew that, or what's the problem with this piece of code, etc..
      On top of that, he makes me (and I assume other people, too) feel like we are part of the Python family; the core team seems close and approachable and I get a feeling of belonging to this line of developers who transformed the way we think about human programming forever.

    • @KurzedMetal
      @KurzedMetal Před 4 lety +1

      No wonder why Python is awesome, they have awesome devs :)

  • @Decessus117
    @Decessus117 Před 9 lety +243

    "There's two kinds of people in the world: people who've mastered dictionaries and total goobers." - Raymond Hettinger

  • @PedroTeixeira
    @PedroTeixeira Před 5 lety +33

    I’ve been using Python for over a decade (informally trained) and haven’t learned so many great tidbits so quickly in a long time. Awesome talk!

  • @gromilla1990
    @gromilla1990 Před 7 lety +124

    "If you are mutating something while you are iterating over it, you are living in a state of sin and you deserve whatever happens to you" :D loled so hard on this one! 20:20

    • @Michael-jq1hl
      @Michael-jq1hl Před 4 lety

      Hi I have recently started studying python, what is that arrow in print i,'-->',color?

    • @burakozdamar
      @burakozdamar Před 4 lety +3

      ​@@Michael-jq1hl it's not a special character or something. he just wanted to print an arrow, that's it. the characters in single or double quotation marks are interpreted as string. so this statement prints 1 --> greens for example.

    • @Michael-jq1hl
      @Michael-jq1hl Před 4 lety

      @@burakozdamar I see, many thanks, I never found the answer to that and now that I see the quotes makes more sense :)

    • @godfather7339
      @godfather7339 Před 2 lety

      @@Michael-jq1hl it's been a year, hope you are doing with python.

    • @Michael-jq1hl
      @Michael-jq1hl Před 2 lety

      @@godfather7339 hey, kind of. I decided that instead of developing I could recruit Python Devs, so that is how I am using the little skill I have acquired.
      Unfortunately, most Devs think I am like any other recruiter and ignore me xD

  • @brotherlui5956
    @brotherlui5956 Před 5 lety +52

    6 years old but still good advice. Raymond is a great teacher.

    • @GroundZero_US
      @GroundZero_US Před 10 měsíci +1

      @@cirogarcia8958 10 years old, your statement of his statement still stands.

    • @stijnvandensande3579
      @stijnvandensande3579 Před 5 měsíci

      @@GroundZero_US almost 11 now

    • @GroundZero_US
      @GroundZero_US Před 4 měsíci

      @@stijnvandensande3579 • and still holds up

  • @Achrononmaster
    @Achrononmaster Před 5 lety +101

    The ignored() idiom exists in Python 3.4+ but is called suppress():
    # NEW WAY in Python 3.4+:
    from contextlib import suppress
    with suppress(OSError):
    os.remove('foo.txt')

    • @locanix
      @locanix Před 4 lety

      Thanks! I was wondering why I couldn't find it

    • @rusca8
      @rusca8 Před 2 lety

      +

  • @stasbovanenko
    @stasbovanenko Před 4 lety +1

    Amazing talk ! Probably best I've heard in all 26 year I'm coding. A handful of smiles and headful of HQ knowledge. Thank you, Raymond !

  • @sandeshgowdru8869
    @sandeshgowdru8869 Před 10 měsíci +1

    10 years old video, and it is gold.

    • @chrstfer2452
      @chrstfer2452 Před 9 měsíci

      Some minor differences, izip is just zip now i guess

  • @merlin2600
    @merlin2600 Před 5 lety +32

    At 44:00, The `with ignored(OSError)` has been renamed to `with suppress(OSError)` and is from the contextlib package.

  • @thatguy1000001
    @thatguy1000001 Před 10 lety +16

    I literally just watched this a second time for the entertainment value! Raymond Hettinger is hilarious! Not only was watching this a lot of fun, but super-informative and efficient.
    I'm going to go check out some of his other talks. If I'm gonna learn python, it might as well be from this dude!

    • @tdao9741
      @tdao9741 Před 2 lety +1

      And how’s yours python 7 years later?

  • @kumarc634
    @kumarc634 Před 8 lety +425

    at 7:22 it should not
    for i, color in enumerate(colors):
    print i,'--->',color[i]
    but
    for i, color in enumerate(colors):
    print i,'--->',color
    may be printing mistake

    • @mByyurt
      @mByyurt Před 7 lety +46

      I was here to write that. Nice catch.

    • @a8lg6p
      @a8lg6p Před 7 lety +37

      I was about to make that comment. Either way works the same, but it's exactly what he's been telling us not to do, so clearly a mistake.

    • @user-rl3pe1cb8c
      @user-rl3pe1cb8c Před 6 lety +30

      Also stopped the video to see if someone has pointed this out already)

    • @aikimark1955
      @aikimark1955 Před 6 lety +1

      Do you think that Raymond is reading these comments?

    • @EyeIn_The_Sky
      @EyeIn_The_Sky Před 6 lety

      I think you do have to have the: print(i,'-->', color [i]) otherwise you will just get all the colours beside each index in a row repeated 4 times. Unless this is just in Python 3?

  • @manojbisht7018
    @manojbisht7018 Před 7 lety +48

    Documented version gist.github.com/JeffPaine/6213790

  • @snowblindu
    @snowblindu Před 8 lety +279

    I wish this guy was my teacher

    • @hdggoyo7423
      @hdggoyo7423 Před 8 lety +2

      +snowblindu he's cocky and not interesting at all

    • @PhilGerb93
      @PhilGerb93 Před 8 lety +17

      +hd ggoyo pffft, hater. I wish he was my teacher too!

    • @jfolz
      @jfolz Před 8 lety +42

      +hd ggoyo He added a lot of the cool stuff to Python so he gets to be cocky.

    • @hdggoyo7423
      @hdggoyo7423 Před 8 lety +6

      +riDDimann i was just joking, he looks like a cool dude, chill guys

    • @AlfredEssa
      @AlfredEssa Před 6 lety +7

      He is cocky, but he knows his shit. If you are going to be a hater, prove your chops.

  • @MrTigerstyle80
    @MrTigerstyle80 Před 9 lety +10

    This dude is off the hook smart.

  • @torgeirlaurvik8195
    @torgeirlaurvik8195 Před 3 lety +1

    This video was the video that got me interested in writing ideomatic Python and iterators. I've revisited it now and then ever since 1. year at uni. Amazing lecturer.

  • @shushens
    @shushens Před 9 lety +19

    Love this guy. I was quite disappointed when it ended. I was so expecting a much much longer video :D

    • @ceestimmerman9785
      @ceestimmerman9785 Před 6 lety

      You could read the "What's New" articles instead: docs.python.org/3/whatsnew/index.html

    • @jacobschmidt
      @jacobschmidt Před 4 lety +1

      @@ceestimmerman9785 it's not quite the same!

  • @VibeWithSingh
    @VibeWithSingh Před 8 lety +6

    This presentation is simply awesome !! hands down !!

  • @matthewwatts9693
    @matthewwatts9693 Před 9 lety +11

    This is a must see video for an python developer. Fantastic!

  • @MrCoolnamehuh
    @MrCoolnamehuh Před 10 lety +1

    Found this to be one of the most useful python talks thus yet on youtube.

  • @VladimirSanchez
    @VladimirSanchez Před 10 lety +2

    I can listen to Mr Hettinger all the time!!!
    Thanks for the presentation and your contributions.

  • @MrTigerstyle80
    @MrTigerstyle80 Před 5 lety +3

    Dude’s kind of a colossal badass. Not even mad at the Casey Kasem vibe at all. He’s a beast.

  • @borgonzalezusach9263
    @borgonzalezusach9263 Před 10 lety

    This man is technically brilliant and hilarious. Not something you see everyday. Thanks for the nice speech!!

  • @joedempseysr.3376
    @joedempseysr.3376 Před 6 lety

    I thoroughly enjoyed your presentation! Take the old-fashioned, ugly, slow way of doing things and instead do the new-fangled, beautiful, fast way. I love it!

  • @guhkunpatata3150
    @guhkunpatata3150 Před 2 lety

    Where have I been All this time only to find this GEM right now ??? This video is very great and a MUST WATCH for any pythonista

  • @richieKostenko
    @richieKostenko Před 9 lety +4

    This is probably the best presented programming video I've ever seen. Thanks!

  • @mpgovinda
    @mpgovinda Před 4 lety

    Just one video and learned more things worth years... great tips!

  • @CraigPerry
    @CraigPerry Před 10 lety +1

    Raymond's an entertaining, engaging presenter. Good mix of material here, definitely came away richer for the experience.

  • @miguelvasquez9849
    @miguelvasquez9849 Před rokem

    this is why i love python, the ways to write clean code.

  • @77aHeB
    @77aHeB Před 2 lety +2

    First - this is an awesome piece, I didn't knew Raymond Hettinger was to be blamed for so many iterators in python, and I truthfully love this guy now that I know what he did for most of my days! :)
    There is a small bug in 15:58 onwards due to the missing tgt variable, but the more important thing is that it can get even more beautiful (IMHO) from:
    def find(seq, target):
    for i, value in enumerate(seq):
    if value == target:
    break
    else:
    return -1
    return i
    to this:
    def find(seq, target):
    for i, value in enumerate(seq):
    if value == target:
    return i
    return -1
    not that the for/else is not nice in some other cases, this was probably just not the right example for it.

  • @hoanghaipham4318
    @hoanghaipham4318 Před 3 lety +1

    What amazing techniques! Why I haven't seen this sooner. Thank you a lots

  • @jasonstapley6822
    @jasonstapley6822 Před 5 lety

    That talk was amazing. Will need to watch this video a few times.

  • @toomuchtruth
    @toomuchtruth Před 6 lety +1

    "Start open source contributions to project by going in and placing doc strings". This is a brilliant idea! I've wanted to start contributing for so long but didn't feel I have the skills, I'm gonna do this!!!

    • @bonbonpony
      @bonbonpony Před 5 lety

      Yes, do it. There's a lot of extremely crappy documentation for Python modules :q

  • @anAlokDubey
    @anAlokDubey Před 3 lety

    I m watching 7 years old video and leaning lots of amazing things.... :-) you are really great.

  • @eduardoherrera3696
    @eduardoherrera3696 Před 4 lety

    Thank you so much for this wonderful lecture. I think this insight might be helpful . As a Novice programer I had a few issues understanding how some of core functions in Python worked and what was because the people who taught me where using derived language examples. Phrases like "if you've code an another language....". But I haven't well time passed I learned the hard way later got to this video and inevitably think that things would been a lot easier for me if this lecture was around.

  • @Krazness
    @Krazness Před 7 lety +2

    this may be the best python video I've seen online

    • @bonbonpony
      @bonbonpony Před 5 lety

      Yeah, there should be a video like this one for every language out there.
      One can learn much more about how to write clean code from such videos than from any crappy tutorial.

  • @espeon91
    @espeon91 Před 9 lety

    Really insightful video. Didn't even notice the time fly by. Already feel better as a Python programmer.

  • @mattralston4969
    @mattralston4969 Před 4 lety +1

    Thank you for the lecture, sir. I appreciate the review of some critical "dos and don'ts" for beginners.

  • @DmitriiGlandarius
    @DmitriiGlandarius Před 7 lety +16

    Very nice. In just 50 minutes I learned moe then in the last month.

  • @f1nt4n2k8
    @f1nt4n2k8 Před 9 lety

    Amazing stuff! Love the functional style entering Python

  • @darkopz
    @darkopz Před 5 lety +2

    Initially I refuse to believe any developer wouldn’t know what “in” semantically means, but then I’m reminded of all the code I’ve seen.

    • @bonbonpony
      @bonbonpony Před 5 lety +3

      Most programmers nowadays have just learnt to parrot other people's code without any actual in-depth understanding of what it does or why is its syntax the way it is. Mostly "thanks to" online tutorials, like those on Udemy or alike where blind lead the blind :q I still have people giving me weird looks when I write C++ code like this:
      if (a == b) do_something();
      else do_something_else();
      instead of wrapping the functions into braces, because they think that those braces are part of the syntaxt and have to be there. Or if I write something like this:
      cout

  • @derekeden1163
    @derekeden1163 Před 4 lety

    probably best python video ive ever seen

  • @GraficaObscura
    @GraficaObscura Před 11 lety

    Wow - amazing talk - informative, concise, immediately useful. Thank you.

  • @mingosutu
    @mingosutu Před 4 lety +8

    In python you make your video with audience, clapping, laughing and commenting. Python is fantastic.

  • @mountainhobo
    @mountainhobo Před 10 lety +11

    Love Raymond's presentation style.

  • @sudiptochatterjee
    @sudiptochatterjee Před 6 lety

    Thanks a lot for this video. Raymond is an awesome guy.

  • @3ombieautopilot
    @3ombieautopilot Před 10 lety

    This is priceless! Thanks for the upload.

  • @RichardGrigonis
    @RichardGrigonis Před 2 lety

    I'm so glad that he said we should rename For as ForEach. I suggested that online some years ago, and boy did I get lambasted by everybody!

  • @AbdelazizYakout
    @AbdelazizYakout Před 9 lety

    great lesson and great presenting style!!

  • @AhmedBalfaqih
    @AhmedBalfaqih Před 9 lety +1

    I kept smiling. Thank you.

  • @defined_user
    @defined_user Před 3 lety

    Such a great video! Many thanks!

  • @nishankbani3257
    @nishankbani3257 Před 6 lety

    Excellent presentation on Python code writing.

  • @Alex_Khouri
    @Alex_Khouri Před 6 lety

    Amazing video - thank you so much!

  • @rusca8
    @rusca8 Před 2 lety

    Man, this is awesome.

  • @JamesSKim
    @JamesSKim Před 10 lety +3

    This lecture is very nice to learn advanced Python programming. After I watched this video, I realize that I have used Python almost like using C. I have used C and C++ as well as Matlab. Hence, I thought that many approaches in Python will be not much different from the conventional languages except specialized keywords. However, I realize that grammars in Python can be different from that in C.

  • @yuli3873
    @yuli3873 Před 2 lety +1

    So this man made enumerate()? I love him from now on!

  • @SeqMod
    @SeqMod Před 9 lety +1

    this guy is really helpful. BLESS

  • @TheXutraks
    @TheXutraks Před 10 lety +1

    Amazing video, really good video and tips, thanks

  • @jnuxadrian
    @jnuxadrian Před 9 lety +39

    I think that 'return newfunc(*args)' on line 6 must be replaced by 'return saved[args]' at 41:12

  • @bigtallslim
    @bigtallslim Před 11 lety

    Great presentation!

  • @AliSattarBarani
    @AliSattarBarani Před 4 lety

    Amazing Slideshow

  • @AJ-et3vf
    @AJ-et3vf Před rokem

    great video. thank you

  • @jakykong
    @jakykong Před 4 lety +4

    He even said I would fight him on this, but haha, oh man, the comparison functions are something I absolutely *miss*, even if I really agree with almost everything else he said and have been writing idiomatic Python for ages. I ran into a real problem the other day where comparisons were a much cleaner solution than the less obvious and potentially much slower multi-pass sorting: Sorting on multiple keys with independent ascending or descending orders.
    You can sort a plain tuple without any fuss if you don't mind that every key is ascending or every key is descending. But if you want to mix those, you have to do it in multiple passes, starting from the least significant key to the most significant key, and rely on timsort's stability to get the job done -- or, IMO, cleaner -- if this were Python 2, I could have expressed the comparison function with appropriate ascending/descending signs in a simple if/elif/else return 0 chain, and rapidly expressed both the intent and the functional result quickly.
    Python 3 even provides a conversion utility that creates wrapper objects on the fly to facilitate this need, which is a bizarrely convoluted way to accomplish what simply allowing either key function or comparison function would have.

  • @anubhav9476
    @anubhav9476 Před 3 lety +1

    Man even though this is not quite as relevant to me today, I really love his presentation style

  • @SanteeRocks
    @SanteeRocks Před 3 lety +1

    This guy is a legend

  • @RanjitKrishnanNair
    @RanjitKrishnanNair Před 4 lety +1

    This was amazing!

  • @SvenSiegmund
    @SvenSiegmund Před 9 lety +1

    I love the people who make Python! Thanks for this most enlightening talk.

  • @EdwardBriggler
    @EdwardBriggler Před 11 lety

    brilliant talk, thanks for sharing!

  • @erik7726
    @erik7726 Před 9 lety

    Very good video, helps writing efficient and clean Python!

  • @jaashutosh
    @jaashutosh Před 7 lety

    Great Presentation.

  • @dmknght8946
    @dmknght8946 Před 5 lety

    Many thanks! It is still useful for me!

  • @dpo357
    @dpo357 Před 9 lety

    I learned a LOT with this video, Kudos Raymond Hettinger
    15:52 for/else, I've always wanted this. For a long time now, I've replicated this behavior in Java enclosing the for inside try/catch and throwing a custom exception of the kind 'BrokenLoop', which looks waaay better, despite feeling forced and inappropriate. Huge like.

  • @purplecrayon7281
    @purplecrayon7281 Před 2 lety

    I love this man! He should write a book on clean Python code, if he hasn't already.

  • @johncongerton7046
    @johncongerton7046 Před 11 měsíci

    Most enlightening talk

  • @uthoshantm
    @uthoshantm Před 4 lety +1

    Note that these functional constructs are now in every mainstream language such as C# and Java. So, it's not that Python has a specific capability, but experienced Python programmers do encourage the use functional constructs. This video is an example.

  • @BillTubbs
    @BillTubbs Před 8 lety +25

    There's an error on the slide at 7:13 titled 'Looping over a collection and indices'. Last line should be:
    print i, '-->', color

    • @vanshdeep91
      @vanshdeep91 Před 7 lety

      Bill Tubbs both have the same meaning brother be it color or colors [i]

    • @BillTubbs
      @BillTubbs Před 7 lety +4

      Both have the same result but using the color iterator that enumerate creates is 'faster and beautiful' as he points out.

  • @PramodL
    @PramodL Před 10 lety

    Can't recommend this talk enough. Must watch to take your python chops to the next level.

  • @movax20h
    @movax20h Před 4 lety +1

    "with ignored(OSError)". So cool. TIL. In Python 3, it is called "with suppress(...)". You can list multiple exception types too.

  • @gibbostooth
    @gibbostooth Před 5 lety

    43:47 "ignored" is now called "suppress" and it's part of "contextlib" library.

  • @feiyuan7607
    @feiyuan7607 Před 9 lety

    Great speech!

  • @mesbahiali4586
    @mesbahiali4586 Před 9 lety

    What a great lecture :D

  • @MacroAggressor
    @MacroAggressor Před 4 lety +1

    This is great! I have a *much* better understanding of iterators and their purpose now. Thanks, Mr. Hettinger.
    edit: How did I not know about dictionary comprehensions?!? (21:05) That is AWESOME!!

    • @dgh25
      @dgh25 Před rokem

      idk, kinda standard stuff in CS

  • @BryanChance
    @BryanChance Před 2 lety +1

    Amazing talk..more professors like him. :) I'm starting to appreciate Python now.
    EDIT: Wow

  • @anonvigil628
    @anonvigil628 Před 4 lety +1

    This needs updated for Python 3 - I have to go hunting for which of his recommendations are still valid. The video could at least have footnotes that say which still applies in Py3.

  • @StevenAkinyemi
    @StevenAkinyemi Před rokem

    I come back to this every few years. 😅

  • @VoicelessRabbit
    @VoicelessRabbit Před 5 lety

    OMG... I have been doing hacks in python for loops trying to do what enumerate does... THANK YOU!!!

  • @rishabhbohra
    @rishabhbohra Před 4 lety

    Give this man a Python Salute!

  • @junivensaavedra882
    @junivensaavedra882 Před rokem

    Superb content. I learn a lot. :)

  • @orkoxy
    @orkoxy Před 10 lety +19

    At 7:26, it should be
    for i, color in enumerate(colors):
    print i, '-->', color

    • @Michael-jq1hl
      @Michael-jq1hl Před 4 lety +2

      Hi I have recently started studying python, what is that arrow in print i,'-->',color?

    • @2adamast
      @2adamast Před 4 lety

      Michael the arrow is a string

  • @raphael3666
    @raphael3666 Před 8 lety +1

    I love this guy

  • @TheGIXL
    @TheGIXL Před 5 lety +7

    41:16 it should be "return saved[args]" instead of "return newfunc(*args)"

  • @ameyms
    @ameyms Před 10 lety +1

    Unbelievably awesome talk!

  • @lordtejas9659
    @lordtejas9659 Před 3 lety +1

    40:26 that one guy is really enjoying it:)

  • @user-pr5jm1pe1r
    @user-pr5jm1pe1r Před 4 lety

    pretty good, thank you

  • @eskays9840
    @eskays9840 Před 7 lety

    very much required for a beginner like and why would someone dislike this ....

  • @gibson_pbacademy
    @gibson_pbacademy Před 6 lety

    Just stumbled across your video mate, really love the content. Liked straight away, We should connect!

  • @Blinkingkitten
    @Blinkingkitten Před 7 lety +16

    "I don't tweet when I arrived at an airport...or anything like that" I guess things change , because this is his most recent tweet: twitter.com/raymondh/status/797267421568634880

  • @blenderpanzi
    @blenderpanzi Před 9 lety +1

    27:50 I think doing this is so common, there should be a group_by(sequence, keyfunc). You could then call it with group_by(names, len). There should also be an index_by(sequence, keyfunc). These functions would be trivial to implement, but nevertheless they are common and useful enough to be put in some standard lib (collections?).