When Python Practices Go Wrong - Brandon Rhodes - code::dive 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
    @YoutubeHandlesSuckBalls2 жыл бұрын

    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
    @mystackquest2 жыл бұрын

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

  • @fenryrtheshaman
    @fenryrtheshaman3 жыл бұрын

    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.

  • @DaVince21
    @DaVince212 жыл бұрын

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

  • @Jeyekomon
    @Jeyekomon2 жыл бұрын

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

  • @dariuszspiewak5624

    @dariuszspiewak5624

    Жыл бұрын

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

  • @dariuszspiewak5624
    @dariuszspiewak5624 Жыл бұрын

    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.

  • @dearheart2
    @dearheart2 Жыл бұрын

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

  • @nishparadox
    @nishparadox3 жыл бұрын

    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.

  • @RoamingAdhocrat
    @RoamingAdhocrat3 жыл бұрын

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

  • @MacHalaG

    @MacHalaG

    3 жыл бұрын

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

  • @usmonbekravshanov6933

    @usmonbekravshanov6933

    2 жыл бұрын

    Me too)

  • @bobweiram6321
    @bobweiram6321 Жыл бұрын

    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 Жыл бұрын

    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!

  • @ChongFrisbee
    @ChongFrisbee2 жыл бұрын

    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 жыл бұрын

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

  • @vancomycinb1193

    @vancomycinb1193

    Жыл бұрын

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

  • @ChongFrisbee

    @ChongFrisbee

    Жыл бұрын

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

  • @ChrisBNisbet
    @ChrisBNisbet2 жыл бұрын

    "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 жыл бұрын

    precisely 👌

  • @leogama3422

    @leogama3422

    Жыл бұрын

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

  • @acasualviewer5861

    @acasualviewer5861

    Жыл бұрын

    @@leogama3422 or type hints

  • @M1911jln
    @M1911jln3 жыл бұрын

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

  • @DaVince21
    @DaVince212 жыл бұрын

    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.

  • @sb-jo2ch
    @sb-jo2ch Жыл бұрын

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

  • @aben7810
    @aben78102 жыл бұрын

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

  • @osquigene
    @osquigene3 жыл бұрын

    "With great power comes great responsibility"

  • @willcoder
    @willcoder2 жыл бұрын

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

  • @gargshishir3
    @gargshishir33 жыл бұрын

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

  • @greenya84
    @greenya842 жыл бұрын

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

  • @jankinsics
    @jankinsics4 жыл бұрын

    Thanks so much. It’s really great presentation.

  • @acasualviewer5861
    @acasualviewer5861 Жыл бұрын

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

  • @mohamedelamineathmani1637
    @mohamedelamineathmani16373 жыл бұрын

    Great conference, enjoyed every single bit of it

  • @kidsforcode3052
    @kidsforcode30523 жыл бұрын

    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.

  • @TNeulaender
    @TNeulaender2 жыл бұрын

    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

  • @gargshishir3
    @gargshishir33 жыл бұрын

    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

  • @driziiD
    @driziiD4 жыл бұрын

    love the bound method example

  • @ChinmayaPanda1007
    @ChinmayaPanda10074 жыл бұрын

    Nice jab at flask for using global variables encapsulating requests.

  • @zustaz

    @zustaz

    4 жыл бұрын

    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 жыл бұрын

    ​@@government_authority I'd recommend "Simple Made Easy" by Rich Hickey as an exploration of what's wrong with this line of thinking kzread.info/dash/bejne/oa2orppxYdOTnrQ.html Developer convenience in the short term doesn't lead to long term maintainable systems

  • @ChinmayaPanda1007

    @ChinmayaPanda1007

    3 жыл бұрын

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

  • @pjmakey
    @pjmakey3 жыл бұрын

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

  • @Altirix_
    @Altirix_3 жыл бұрын

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

  • @thinkingcitizen
    @thinkingcitizen2 ай бұрын

    I love this guy, read many chpters of his book

  • @vladimirkraus1438
    @vladimirkraus14383 жыл бұрын

    the words are pure gold

  • @iankaranja7765
    @iankaranja77653 жыл бұрын

    Amazing talk.

  • @KanishkSingh07
    @KanishkSingh076 ай бұрын

    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

  • @TheUggeli
    @TheUggeli2 жыл бұрын

    Love this dudes voice

  • @pward17
    @pward173 жыл бұрын

    I cannot click a Brandon Rhodes talk quick enough.

  • @astrolillo
    @astrolillo Жыл бұрын

    the Dr House of Python, good one

  • @kiwiate
    @kiwiate3 жыл бұрын

    thanks for all the python tricks!

  • @ypaut
    @ypaut2 жыл бұрын

    Awesome!

  • @manonthedollar
    @manonthedollar Жыл бұрын

    bruh the guy introducing him

  • @PhilippeCarphin
    @PhilippeCarphin3 жыл бұрын

    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.

  • @jirehjirehjireh
    @jirehjirehjireh2 жыл бұрын

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

  • @josh___something

    @josh___something

    2 жыл бұрын

    Not mutually exclusive

  • @PanduPoluan
    @PanduPoluan2 жыл бұрын

    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 жыл бұрын

    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

  • @wucf20
    @wucf20 Жыл бұрын

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

  • @StrangeIndeed
    @StrangeIndeed3 жыл бұрын

    1:25 - the talk begins

  • @ErdosainNueve
    @ErdosainNueve3 жыл бұрын

    The George Carlin of Python! Amazing.

  • @user-zi2zv1jo7g
    @user-zi2zv1jo7g3 ай бұрын

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

  • @DrDeuteron
    @DrDeuteron10 ай бұрын

    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.

  • @Southpaw101
    @Southpaw1013 жыл бұрын

    Great talk !

  • @VCR47527
    @VCR47527 Жыл бұрын

    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

  • @TomMcKeesick
    @TomMcKeesick3 жыл бұрын

    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 жыл бұрын

    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 жыл бұрын

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

  • @ashrasmun1
    @ashrasmun12 жыл бұрын

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

  • @littleconan7929
    @littleconan7929 Жыл бұрын

    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.

  • @medthehatta
    @medthehatta4 жыл бұрын

    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 жыл бұрын

    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 жыл бұрын

    Take a look at Brandon's talk about Clean Architecture in Python: kzread.info/dash/bejne/dn6ox8htYZO-kbA.html

  • @darthmoomoo

    @darthmoomoo

    4 жыл бұрын

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

  • @medthehatta

    @medthehatta

    4 жыл бұрын

    @@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 жыл бұрын

    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.

  • @deludank
    @deludank3 жыл бұрын

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

  • @ailuros_
    @ailuros_2 жыл бұрын

    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

  • @HrHaakon
    @HrHaakon2 жыл бұрын

    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

    Жыл бұрын

    @@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
    @huskielunar51852 жыл бұрын

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

  • @CompilationError
    @CompilationError3 жыл бұрын

    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 жыл бұрын

    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

    3 жыл бұрын

    @MacHalaG thanks man. ☺️☺️

  • @morthim
    @morthim2 жыл бұрын

    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"

  • @andreienache6290
    @andreienache62903 жыл бұрын

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

  • @duskbegins3846
    @duskbegins38463 жыл бұрын

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

  • @malayagr
    @malayagr2 жыл бұрын

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

  • @SimonJackson13
    @SimonJackson13 Жыл бұрын

    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?

  • @alan2here
    @alan2here3 жыл бұрын

    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 жыл бұрын

    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

  • @familievermeulen5499
    @familievermeulen54992 жыл бұрын

    23:00

  • @sirius7584
    @sirius75842 жыл бұрын

    18:10 Explicit is better than implicit

  • @driziiD
    @driziiD4 жыл бұрын

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

  • @felixfourcolor
    @felixfourcolor3 ай бұрын

    Type hints and type checkers exist 🤦‍♂️

  • @heroe1486

    @heroe1486

    3 ай бұрын

    Not in old codebases tho

  • @futurepython6774
    @futurepython67742 жыл бұрын

    python/django/mysql新教程: python基础:kzread.info/dash/bejne/mWqG0LWLdM3KZbA.html

  • @channeldsr9983
    @channeldsr99833 жыл бұрын

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

  • @_jdfx
    @_jdfx11 ай бұрын

    white background? seriously?

  • @johnny5941

    @johnny5941

    Ай бұрын

    More readable

  • @bradenbigham9875
    @bradenbigham9875 Жыл бұрын

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

Келесі