When Python Practices Go Wrong - Brandon Rhodes - code::dive 2019

Sdílet
Vložit
  • čas přidán 30. 12. 2019
  • Abstract
    Just because a programming pattern or convention becomes popular doesn’t always mean it’s a good idea! Let’s dig into the consensus the Python community has built around what constitutes “Pythonic” code and look at the cases where the conventional wisdom is wrong. In the process, we’ll learn how code in a dynamic language can become more readable and more effective.
    About the Speaker - Brandon Rhodes
    Brandon draws on more than 20 years experience with Python and 30 years with Unix to distill design patterns and effective practices for the audiences of his talks, writing, and training. He is the maintainer of the PyEphem and Skyfield astronomy libraries for Python as well as numerous smaller projects. He is a Fellow of the Python Software Foundation, on whose behalf he served as the volunteer chair of PyCon 2016-2017.
  • Věda a technologie

Komentáře • 117

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

    One lesson that it is vital to learn when programming in any language is that the person reading the code in the future is never you, and so you need to make your code clear to read.
    Even if the person reading the code is you, enough time has passed such that the person reading the code is never the same person.

  • @mystackquest
    @mystackquest Před 2 lety +15

    28:53 ---> Django
    29:27 ---> Flask
    30:56 ---> Last two problems are hectic when using Flask
    31:35 ---> which is that one.

  • @fenryrtheshaman
    @fenryrtheshaman Před 3 lety +23

    Great talk, though I'm sad he didn't mention type hinting and instead wanted to put cruft in bool checks to imply typing. An implication isn't explicit, if you want explicit typing then explicitly hint it.

  • @Jeyekomon
    @Jeyekomon Před 2 lety +21

    The example in 16:20 becomes perfectly clear and unambiguous if you add type hints to the function parameters.

    • @dariuszspiewak5624
      @dariuszspiewak5624 Před rokem +3

      Yeah... but there surely are people who think type hints are a waste of time...

  • @dariuszspiewak5624
    @dariuszspiewak5624 Před rokem +4

    One lesson to take is this: write your code in such a way that you never need to write unit tests with 'patch.' If you can do it, it means you definitely know what decoupling is and how to use it properly.

  • @bobweiram6321
    @bobweiram6321 Před rokem +5

    Spend sometime to measure just how much code you type. You'll quickly find you type a little, think a lot, and type a little, etc. You have to ask what does brevity save you? It doesn't make the coding process any faster, especially if it's the first iteration. It only creates a false perception of doing more with less. You may have less code, but it takes more mental effort to understand.

  • @dearheart2
    @dearheart2 Před rokem +5

    Grown up with explicit and readable programming, I can only agree with what he says.

  • @RoamingAdhocrat
    @RoamingAdhocrat Před 3 lety +18

    I've watched hours of videos about testing but didn't understand how to do mocking or patching until this video.

  • @nishparadox
    @nishparadox Před 3 lety +18

    Every time I watch/read from Brando Rhodes, I get more wrong about my own design patterns. Nevertheless, it helps me to think clearly about ideas/problems/solutions from another vantage point. This talk is (now) easily my favorite programming talk.

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

    I love the way this guy explains everything. Not only is it very insightful, he brings it in a very fun, personable way.

  • @ChongFrisbee
    @ChongFrisbee Před 2 lety +27

    Brandon Rhodes: "Python is always in danger of becoming a type desert"
    Python's new features: "Type hints, type hints, and moar type hints"

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

      This talk must have woken up some Python devs across the years. 😄

    • @vancomycinb1193
      @vancomycinb1193 Před rokem +1

      thread necromancy, but yes.
      def foo(x: int) is helpful. Very very helpful

    • @ChongFrisbee
      @ChongFrisbee Před rokem

      @@vancomycinb1193 function name could've helped there, also, 😂

  • @ChrisBNisbet
    @ChrisBNisbet Před 2 lety +18

    "Explicit is better than implicit" makes the code reader's job easier, and we apparently spend much more time reading code than writing it.
    The idea of making things obvious to the reader seems like a good one to me.

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

      precisely 👌

    • @leogama3422
      @leogama3422 Před rokem +1

      In the example he showed, simply naming the variable "n_users" instead of "users" would solve the mystery. Just two extra characters...

    • @acasualviewer5861
      @acasualviewer5861 Před rokem +1

      @@leogama3422 or type hints

  • @thanhtan030t
    @thanhtan030t Před rokem +2

    Now I'm able to know why Python is easy to learn. Not because it has simple syntax, but because of these awesome teachers. Thanks!

  • @ChinmayaPanda1007
    @ChinmayaPanda1007 Před 4 lety +12

    Nice jab at flask for using global variables encapsulating requests.

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

      Thanks also for covering the issue with "major python framework" mixins obsession

    • @government_authority
      @government_authority Před 3 lety

      no need to pass a request object makes it quite elegant. users benefit from this design, what`s more important.

    • @t3hjames
      @t3hjames Před 3 lety +3

      ​@@government_authority I'd recommend "Simple Made Easy" by Rich Hickey as an exploration of what's wrong with this line of thinking czcams.com/video/oytL881p-nQ/video.html
      Developer convenience in the short term doesn't lead to long term maintainable systems

    • @ChinmayaPanda1007
      @ChinmayaPanda1007 Před 3 lety +6

      @@government_authority making path params enter the normal way and others via a global request object makes an endpoint seem more than what it really is: a transformation, from request to response. This is a functional way of thinking about it and it goes a long way opposed to (questionable) visual appeal.

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

    Great talk. Not so about python, but about how to write long term code for humans.

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

    I finally found someone else who shares my skepticism of mocks.

  • @sb-jo2ch
    @sb-jo2ch Před rokem +5

    The first one minute is me in any kind of public speaking

  • @willcoder
    @willcoder Před 2 lety +2

    I agree. Moving a problem is often viewed as "solving" a problem, instead of merely relocating it. (re: config in code, databases etc at import time).

  • @aben7810
    @aben7810 Před rokem +3

    Great talker. Makes you happy to write more lines a day

  • @osquigene
    @osquigene Před 2 lety +4

    "With great power comes great responsibility"

  • @gargshishir3
    @gargshishir3 Před 3 lety +2

    Loved the pipeline pattern - it’s exactly what Gary Bernhardt talks about - having data boundaries between logic cores. Makes testing a breeze as well.

  • @pjmakey
    @pjmakey Před 3 lety +7

    Just as always Brandon makes me wonder how wrong is my code and system and try to improved it

  • @DaVince21
    @DaVince21 Před 2 lety +9

    I've been learning Rust and I feel, comparatively, it tries very hard to discourage you from using poor or insecure design patterns to the point of entirely disallowing them. Instead, the compiler is very intelligent and lets you know how to deal with the problem. It's a lot more difficult to learn but I feel as a result it avoids various issues as outlined by this talk.

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

    everything about this talk is so perfect, especially as someone coming from C# where you can’t patch io library code, always felt uncomfortable with the amount of “freedom” in dynamic languages

  • @kidsforcode3052
    @kidsforcode3052 Před 3 lety +7

    Thanks for this excellent talk! It really sparks myself to think over my own design pattern. Need to do more pipelining! Or, do I? Also, it appreciates the decades-long learning curve behind python coding patterns.

  • @acasualviewer5861
    @acasualviewer5861 Před rokem +2

    Very good explanation of the Bridge pattern.. I hadn't seen it explained that way before.

  • @driziiD
    @driziiD Před 4 lety +2

    love the bound method example

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

    Thanks so much. It’s really great presentation.

  • @mohamedelamineathmani1637

    Great conference, enjoyed every single bit of it

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

    interesting talk, a lot of stuff i agree about and a few thing i didn't know were possible. also explained really well

  • @TNeulaender
    @TNeulaender Před 2 lety +9

    Around 17:00 is all a problem you wouldn't have if you would just type. And you could easily go back to pythonic code

  • @TheUggeli
    @TheUggeli Před 2 lety +2

    Love this dudes voice

  • @vladimirkraus1438
    @vladimirkraus1438 Před 3 lety

    the words are pure gold

  • @thinkingcitizen
    @thinkingcitizen Před měsícem +1

    I love this guy, read many chpters of his book

  • @KanishkSingh07
    @KanishkSingh07 Před 5 měsíci +2

    Oh god this talk made me so furious! I agree with everything here and I've seen it so often!
    Especially the part about mutability and side efforts

  • @astrolillo
    @astrolillo Před rokem +2

    the Dr House of Python, good one

  • @PanduPoluan
    @PanduPoluan Před 2 lety +9

    Huh. The talk about mixins made me regret the decisions I made for aiosmtpd ... but it's water under the bridge now, the library is getting used more and more widely, and undoing the mixin will break many other programs... oh well 😞

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

      It's never too late to increase the major version and announce breakage and required changes for that major version!

    • @leogama3422
      @leogama3422 Před rokem

      You may keep the old and the new versions in parallel for some time, like python 2 and 3

  • @iankaranja7765
    @iankaranja7765 Před 3 lety

    Amazing talk.

  • @user-zi2zv1jo7g
    @user-zi2zv1jo7g Před 2 měsíci +1

    Yeah you should defenetly use strict typing or basically add the types to variable names instead of making the code less efficient

  • @kiwiate
    @kiwiate Před 2 lety

    thanks for all the python tricks!

  • @manonthedollar
    @manonthedollar Před rokem +6

    bruh the guy introducing him

  • @wucf20
    @wucf20 Před rokem +1

    Just curious: Anyone knows which ThinkPad he was using in the video, please?

  • @VCR47527
    @VCR47527 Před rokem +1

    Potentially hot take: use web frameworks that prioritize extreme succinctness over clarity only when you know framework well and you're prototyping. Make your proof of concept super fast and plan to rewrite the entire thing in earnest if it takes off and you may need to make any changes to it

    • @acasualviewer5861
      @acasualviewer5861 Před rokem

      and often "hidden behaviors" is what makes things hard to understand and increase the learning burden (while at the same time keeping to code concise)
      more verbose frameworks may require more typing, but they can also be easier to learn

  • @ashrasmun1
    @ashrasmun1 Před 2 lety +3

    Ad 1# Why don't you just add a type annotation and use mypy? IMO that's a better approach.

  • @PhilippeCarphin
    @PhilippeCarphin Před 3 lety +6

    That bound method confused me back when I was starting python. I always felt weird passing object.method to frameworks because I thought it would lose the reference to the instance (i.e. the framework or whatever would call my function and it would be missing a self parameter).
    This at the same time as I'm having other troubles with the framework so just get over it by using lambda a,b,c: object.method(a,b,c) then solve the other actual problems. And leaving proof that I did not understand bound methods at that time.

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

    I cannot click a Brandon Rhodes talk quick enough.

  • @ypaut
    @ypaut Před 2 lety

    Awesome!

  • @StrangeIndeed
    @StrangeIndeed Před 3 lety +3

    1:25 - the talk begins

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

    Does anyone else think that he could’ve been a preacher if he weren’t a programmer 😅

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

    I am so bad with dunder call. I make classes to do one then, so I call it do it. Then I can’t find it in the code.

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

    The George Carlin of Python! Amazing.

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

    Well constructed and delivered, and surprisingly funny! "they noticed the code had so much repetition! request, request, request...".
    I'm assuming this is Django (ed: it was Flask, cheers RJM)? As someone that's competent but not expert with web frameworks, I didn't find it pleasant to work with. It matched all of the problems that were mentioned, good to know I wasn't going crazy haha

    • @robbert-janmerk6783
      @robbert-janmerk6783 Před 3 lety +7

      No, in Django the request is either passed as an argument. You don't import the request object. You do import the request object in Flask. Which is one of the reasons I dislike Flask and that frameworks like FastAPI are just better overall.

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

      ​@@robbert-janmerk6783 oh you're totally right, I got them mixed up!
      Must have buried the memories pretty deep haha

  • @littleconan7929
    @littleconan7929 Před rokem +3

    what about shortcuts in __init__.py ?
    Ex:
    from .module_a import A
    It is sometime very convenient to avoid uselessly long imports. Also i understand, it forces the import of modules you may not require.

    • @Moriadin
      @Moriadin Před 10 měsíci

      imo this hides dependencies. Dependencies should always be clear in modules

    • @marcolavagnino966
      @marcolavagnino966 Před 3 měsíci

      get ready to have fun with circular imports if you do this

    • @littleconan7929
      @littleconan7929 Před 3 měsíci

      @@marcolavagnino966 yeah Indeed it may cause this kind or problème if you do not sequence your imports properly.
      But for users it is very convenient : they just have to know a few usefull classes and not the all project structure.

    • @marcolavagnino966
      @marcolavagnino966 Před 3 měsíci

      @@littleconan7929 I feel like that problem is solved by modern code completions, you don't need to know the path to a class to let the editor import it.

  • @medthehatta
    @medthehatta Před 4 lety +2

    Does anybody know what he meant by a "Pipeline" being the final stage in a chain of design improvements to mixins (see: 59:05)? Does this refer to the clean architecture? It's not clear to me how the clean architecture is analogous to using a pipeline.

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

      It is analogous in the sense that if "Window.render" is the I/O part, the layout engine does not need to be aware of it. The layout results are stored in data structures that are then read by window.render in order to render them. This de-couples the two systems.

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

      Take a look at Brandon's talk about Clean Architecture in Python: czcams.com/video/DJtef410XaM/video.html

    • @darthmoomoo
      @darthmoomoo Před 4 lety

      @@zustaz That's here I learned about it. Good resource!

    • @medthehatta
      @medthehatta Před 4 lety +2

      @@zustaz I've seen that talk, I didn't see the analogy. But darthmoo explained it pretty well: it's a pipeline in that "inner" parts of the clean architecture are like "earlier" parts of the pipeline: they don't know what will come after them

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

      When the word pipeline is mentioned, my mind immediately goes to Unix: having various small programs that do one thing and do it well, only to pipe their output to the next program which will work with it further. Decoupling your code into independently functional parts accomplishes the same, but within one program.

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

    Great talk !

  • @deludank
    @deludank Před 3 lety +2

    *Brandon Rhodes - Advice for the Young at [Python]*

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

    lmao I would love him talking about the Ruby object model which is hiper mega super dynamic

    • @leogama3422
      @leogama3422 Před rokem

      What you can modify in a ruby class that you can't in a python class? Asking of pure curiosity

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

    Java has the compiler available at runtime, as well as introspection.
    The idea that Java is a very dynamic language is kind of funny. But making named tuples is deeply against everything Java stands for.
    It's an absolutely horrid thing for larger enterprise systems. If you have to stash say, an email and a message together, make a class for it, name it, document it, and use it. Because ten years later, your code is still running, and someone has a stacktrace with your class in it. And seeing "EmailWithUpdate" makes perfect sense for context, and thus debugging is simpler. You can go and find its usages, see the documentation and so on.
    Just a funny little aside.

    • @dariuszspiewak5624
      @dariuszspiewak5624 Před rokem

      Hm... I don't know Java but have heard many not-the-greatest things about it. Since Python is highly dynamic (Java is definitely not, even though it has some dynamic capabilities; one simple example: in Python you can create attributes on the fly just by typing their names, can you do that in Java?), documentation in Python is something not only standard but required. Almost every function, every class, module, every piece of code... you name it - HAS DOCUMENTATION. And good documentation at that. This is one area that saves Python at the end of the day. I have not seen projects documented as well as Python projects.

    • @HrHaakon
      @HrHaakon Před rokem +1

      @@dariuszspiewak5624
      > I don't know Java
      That's okay, but then...
      > Almost every function, every class, module, every piece of code... you name it - HAS DOCUMENTATION. And good documentation at that.
      My dude, you don't know how good JavaDoc is.

  • @huskielunar5185
    @huskielunar5185 Před 2 lety +3

    I like that they now added type decorators. You can do just "user: list" or alike

  • @CompilationError
    @CompilationError Před 3 lety +2

    I understood some . But most of it are maybe advance . Can anyone tell me . What i need to do to understand all the topics here ? ❤️❤️❤️

    • @hunorportik5618
      @hunorportik5618 Před 3 lety

      First learn a statically typed language. Then come back and learn Python. Then watch this video.

    • @CompilationError
      @CompilationError Před 3 lety

      @@hunorportik5618 i know cpp and java . and also have a good understanding of ds and algo
      thanks .

    • @Altirix_
      @Altirix_ Před 3 lety

      what parts are you unable to understand, knowing that might help give us better info to build your knowlage. you would most likely see things that are shown in this talk in the workplace. that generally you wont see in your own programs or while learning.

    • @MacHalaG
      @MacHalaG Před 3 lety +2

      if you have a problem understanding this, then watch his other lecture "The Clean Architecture in Python". I am not saying it will help you everything from this one, but if you follow it religiously you won't need the parts that you didn't understand here.

    • @CompilationError
      @CompilationError Před 2 lety

      @MacHalaG thanks man. ☺️☺️

  • @SimonJackson13
    @SimonJackson13 Před rokem +2

    Nice and maybe mypy helps, but as a scripting language "the masters of disposable botch efficiency" would have **there** way.

    • @SimonJackson13
      @SimonJackson13 Před rokem

      Pascal needs mocks to fill in the bottom, Fourth has no ui until the logic is made?

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

    now louis c k teaches python! interesting.....

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

    👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻

  • @alan2here
    @alan2here Před 3 lety

    Can I write some python code to run at the start of the program that transforms all my functions that use type hints like this? Including ones in classes.
    def myFunc(count : int, word_box : typing.collection[str]):
    # my code
    def myFunc(count : int, word_box : typing.collection[str]):
    if type(count) is not int or type(word_box) is not typing.collection[str]: raise TypeError
    # my code

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

      You could write a decorator that looks at the type annotations on the function and makes an isinstance call
      Alternatively (and this is the more popular option), you could use mypy to statically check types before you run your code

  • @malayagr
    @malayagr Před 2 lety +2

    I'm pretty sure Django is the web framework getting all the flake here, lol.

  • @sirius7584
    @sirius7584 Před 2 lety +3

    18:10 Explicit is better than implicit

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

    what are those parenthesis doing?!?!? 😂😂

  • @morthim
    @morthim Před 2 lety +3

    50:25
    (subtitles for the part)
    "show me your code and i'll be mystified;
    show me your datastructures, and the code will be obvious"

  • @felixfourcolor
    @felixfourcolor Před 2 měsíci

    Type hints and type checkers exist 🤦‍♂️

    • @heroe1486
      @heroe1486 Před 2 měsíci

      Not in old codebases tho

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

    Ведущий умер в душе

  • @futurepython6774
    @futurepython6774 Před 2 lety

    python/django/mysql新教程:
    python基础:czcams.com/video/g6RnSRDjd5M/video.html

  • @_jdfx
    @_jdfx Před 10 měsíci +2

    white background? seriously?

  • @bradenbigham9875
    @bradenbigham9875 Před rokem

    python code:
    open("furby.py", mode="w").wright(data)
    from furby import run
    run()