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

Поділитися
Вставка
  • Опубліковано 30 гру 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.
  • Наука та технологія

КОМЕНТАРІ • 117

  • @YoutubeHandlesSuckBalls
    @YoutubeHandlesSuckBalls 2 роки тому +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 2 роки тому +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 3 роки тому +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.

  • @ChongFrisbee
    @ChongFrisbee 2 роки тому +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 2 роки тому +1

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

    • @vancomycinb1193
      @vancomycinb1193 Рік тому +1

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

    • @ChongFrisbee
      @ChongFrisbee Рік тому

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

  • @ChrisBNisbet
    @ChrisBNisbet 2 роки тому +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 2 роки тому +1

      precisely 👌

    • @leogama3422
      @leogama3422 Рік тому +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 Рік тому +1

      @@leogama3422 or type hints

  • @dearheart2
    @dearheart2 Рік тому +5

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

  • @DaVince21
    @DaVince21 2 роки тому +5

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

  • @dariuszspiewak5624
    @dariuszspiewak5624 Рік тому +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.

  • @RoamingAdhocrat
    @RoamingAdhocrat 3 роки тому +18

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

    • @MacHalaG
      @MacHalaG 3 роки тому +1

      just to learn that when you need it you probably did something wrong beforehand :D

    • @usmonbekravshanov6933
      @usmonbekravshanov6933 2 роки тому

      Me too)

  • @Jeyekomon
    @Jeyekomon 2 роки тому +21

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

    • @dariuszspiewak5624
      @dariuszspiewak5624 Рік тому +3

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

  • @nishparadox
    @nishparadox 3 роки тому +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.

  • @bobweiram6321
    @bobweiram6321 Рік тому +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.

  • @thanhtan030t
    @thanhtan030t Рік тому +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!

  • @M1911jln
    @M1911jln 3 роки тому +5

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

  • @willcoder
    @willcoder 2 роки тому +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).

  • @sb-jo2ch
    @sb-jo2ch Рік тому +5

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

  • @gargshishir3
    @gargshishir3 3 роки тому +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.

  • @DaVince21
    @DaVince21 2 роки тому +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 3 роки тому +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

  • @aben7810
    @aben7810 2 роки тому +3

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

  • @acasualviewer5861
    @acasualviewer5861 Рік тому +2

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

  • @greenya84
    @greenya84 2 роки тому +6

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

  • @osquigene
    @osquigene 2 роки тому +4

    "With great power comes great responsibility"

  • @ChinmayaPanda1007
    @ChinmayaPanda1007 4 роки тому +12

    Nice jab at flask for using global variables encapsulating requests.

    • @zustaz
      @zustaz 4 роки тому +3

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

    • @government_authority
      @government_authority 3 роки тому

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

    • @t3hjames
      @t3hjames 3 роки тому +3

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

    • @ChinmayaPanda1007
      @ChinmayaPanda1007 3 роки тому +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.

  • @kidsforcode3052
    @kidsforcode3052 3 роки тому +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.

  • @pjmakey
    @pjmakey 3 роки тому +7

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

  • @driziiD
    @driziiD 4 роки тому +2

    love the bound method example

  • @jankinsics
    @jankinsics 4 роки тому +3

    Thanks so much. It’s really great presentation.

  • @Altirix_
    @Altirix_ 3 роки тому +4

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

  • @mohamedelamineathmani1637
    @mohamedelamineathmani1637 3 роки тому +2

    Great conference, enjoyed every single bit of it

  • @TNeulaender
    @TNeulaender 2 роки тому +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

  • @thinkingcitizen
    @thinkingcitizen Місяць тому +1

    I love this guy, read many chpters of his book

  • @vladimirkraus1438
    @vladimirkraus1438 3 роки тому

    the words are pure gold

  • @pward17
    @pward17 3 роки тому +1

    I cannot click a Brandon Rhodes talk quick enough.

  • @manonthedollar
    @manonthedollar Рік тому +6

    bruh the guy introducing him

  • @TheUggeli
    @TheUggeli 2 роки тому +2

    Love this dudes voice

  • @KanishkSingh07
    @KanishkSingh07 5 місяців тому +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

  • @jirehjirehjireh
    @jirehjirehjireh 2 роки тому +10

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

  • @astrolillo
    @astrolillo Рік тому +2

    the Dr House of Python, good one

  • @PanduPoluan
    @PanduPoluan 2 роки тому +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 2 роки тому +5

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

    • @leogama3422
      @leogama3422 Рік тому

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

  • @iankaranja7765
    @iankaranja7765 3 роки тому

    Amazing talk.

  • @ypaut
    @ypaut 2 роки тому

    Awesome!

  • @VCR47527
    @VCR47527 Рік тому +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 Рік тому

      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

  • @wucf20
    @wucf20 Рік тому +1

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

  • @PhilippeCarphin
    @PhilippeCarphin 3 роки тому +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.

  • @kiwiate
    @kiwiate 2 роки тому

    thanks for all the python tricks!

  • @user-zi2zv1jo7g
    @user-zi2zv1jo7g 3 місяці тому +1

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

  • @ErdosainNueve
    @ErdosainNueve 3 роки тому +5

    The George Carlin of Python! Amazing.

  • @medthehatta
    @medthehatta 4 роки тому +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 4 роки тому +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 4 роки тому +4

      Take a look at Brandon's talk about Clean Architecture in Python: ua-cam.com/video/DJtef410XaM/v-deo.html

    • @darthmoomoo
      @darthmoomoo 4 роки тому

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

    • @medthehatta
      @medthehatta 4 роки тому +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 2 роки тому +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.

  • @StrangeIndeed
    @StrangeIndeed 3 роки тому +3

    1:25 - the talk begins

  • @TomMcKeesick
    @TomMcKeesick 3 роки тому +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 3 роки тому +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 3 роки тому +1

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

  • @HrHaakon
    @HrHaakon 2 роки тому +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 Рік тому

      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 Рік тому +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.

  • @ashrasmun1
    @ashrasmun1 2 роки тому +3

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

  • @DrDeuteron
    @DrDeuteron 10 місяців тому +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.

  • @littleconan7929
    @littleconan7929 Рік тому +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 10 місяців тому

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

    • @marcolavagnino966
      @marcolavagnino966 3 місяці тому

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

    • @littleconan7929
      @littleconan7929 3 місяці тому

      @@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 3 місяці тому

      @@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.

  • @Southpaw101
    @Southpaw101 3 роки тому +1

    Great talk !

  • @huskielunar5185
    @huskielunar5185 2 роки тому +3

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

  • @deludank
    @deludank 3 роки тому +2

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

  • @ailuros_
    @ailuros_ 2 роки тому +1

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

    • @leogama3422
      @leogama3422 Рік тому

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

  • @andreienache6290
    @andreienache6290 3 роки тому +1

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

  • @duskbegins3846
    @duskbegins3846 3 роки тому +10

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

  • @morthim
    @morthim 2 роки тому +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"

  • @SimonJackson13
    @SimonJackson13 Рік тому +2

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

    • @SimonJackson13
      @SimonJackson13 Рік тому

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

  • @CompilationError
    @CompilationError 3 роки тому +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 3 роки тому

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

    • @CompilationError
      @CompilationError 3 роки тому

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

    • @Altirix_
      @Altirix_ 3 роки тому

      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 3 роки тому +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 2 роки тому

      @MacHalaG thanks man. ☺️☺️

  • @alan2here
    @alan2here 3 роки тому

    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 3 роки тому +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 2 роки тому +2

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

  • @sirius7584
    @sirius7584 2 роки тому +3

    18:10 Explicit is better than implicit

  • @familievermeulen5499
    @familievermeulen5499 2 роки тому

    23:00

  • @driziiD
    @driziiD 4 роки тому +1

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

  • @felixfourcolor
    @felixfourcolor 2 місяці тому

    Type hints and type checkers exist 🤦‍♂️

    • @heroe1486
      @heroe1486 2 місяці тому

      Not in old codebases tho

  • @channeldsr9983
    @channeldsr9983 3 роки тому +1

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

  • @futurepython6774
    @futurepython6774 2 роки тому

    python/django/mysql新教程:
    python基础:ua-cam.com/video/g6RnSRDjd5M/v-deo.html

  • @_jdfx
    @_jdfx 10 місяців тому +2

    white background? seriously?

  • @bradenbigham9875
    @bradenbigham9875 Рік тому

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