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. - Наука та технологія
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.
28:53 ---> Django
29:27 ---> Flask
30:56 ---> Last two problems are hectic when using Flask
31:35 ---> which is that one.
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.
Brandon Rhodes: "Python is always in danger of becoming a type desert"
Python's new features: "Type hints, type hints, and moar type hints"
This talk must have woken up some Python devs across the years. 😄
thread necromancy, but yes.
def foo(x: int) is helpful. Very very helpful
@@vancomycinb1193 function name could've helped there, also, 😂
"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.
precisely 👌
In the example he showed, simply naming the variable "n_users" instead of "users" would solve the mystery. Just two extra characters...
@@leogama3422 or type hints
Grown up with explicit and readable programming, I can only agree with what he says.
I love the way this guy explains everything. Not only is it very insightful, he brings it in a very fun, personable way.
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.
I've watched hours of videos about testing but didn't understand how to do mocking or patching until this video.
just to learn that when you need it you probably did something wrong beforehand :D
Me too)
The example in 16:20 becomes perfectly clear and unambiguous if you add type hints to the function parameters.
Yeah... but there surely are people who think type hints are a waste of time...
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.
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.
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!
I finally found someone else who shares my skepticism of mocks.
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).
The first one minute is me in any kind of public speaking
Loved the pipeline pattern - it’s exactly what Gary Bernhardt talks about - having data boundaries between logic cores. Makes testing a breeze as well.
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.
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
Great talker. Makes you happy to write more lines a day
Very good explanation of the Bridge pattern.. I hadn't seen it explained that way before.
Great talk. Not so about python, but about how to write long term code for humans.
"With great power comes great responsibility"
Nice jab at flask for using global variables encapsulating requests.
Thanks also for covering the issue with "major python framework" mixins obsession
no need to pass a request object makes it quite elegant. users benefit from this design, what`s more important.
@@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
@@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.
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.
Just as always Brandon makes me wonder how wrong is my code and system and try to improved it
love the bound method example
Thanks so much. It’s really great presentation.
interesting talk, a lot of stuff i agree about and a few thing i didn't know were possible. also explained really well
Great conference, enjoyed every single bit of it
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
I love this guy, read many chpters of his book
the words are pure gold
I cannot click a Brandon Rhodes talk quick enough.
bruh the guy introducing him
Love this dudes voice
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
Does anyone else think that he could’ve been a preacher if he weren’t a programmer 😅
Not mutually exclusive
the Dr House of Python, good one
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 😞
It's never too late to increase the major version and announce breakage and required changes for that major version!
You may keep the old and the new versions in parallel for some time, like python 2 and 3
Amazing talk.
Awesome!
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
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
Just curious: Anyone knows which ThinkPad he was using in the video, please?
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.
thanks for all the python tricks!
Yeah you should defenetly use strict typing or basically add the types to variable names instead of making the code less efficient
The George Carlin of Python! Amazing.
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.
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.
Take a look at Brandon's talk about Clean Architecture in Python: ua-cam.com/video/DJtef410XaM/v-deo.html
@@zustaz That's here I learned about it. Good resource!
@@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
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.
1:25 - the talk begins
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
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.
@@robbert-janmerk6783 oh you're totally right, I got them mixed up!
Must have buried the memories pretty deep haha
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.
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.
@@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.
Ad 1# Why don't you just add a type annotation and use mypy? IMO that's a better approach.
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.
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.
imo this hides dependencies. Dependencies should always be clear in modules
get ready to have fun with circular imports if you do this
@@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.
@@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.
Great talk !
I like that they now added type decorators. You can do just "user: list" or alike
*Brandon Rhodes - Advice for the Young at [Python]*
lmao I would love him talking about the Ruby object model which is hiper mega super dynamic
What you can modify in a ruby class that you can't in a python class? Asking of pure curiosity
👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻
now louis c k teaches python! interesting.....
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"
Nice and maybe mypy helps, but as a scripting language "the masters of disposable botch efficiency" would have **there** way.
Pascal needs mocks to fill in the bottom, Fourth has no ui until the logic is made?
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 ? ❤️❤️❤️
First learn a statically typed language. Then come back and learn Python. Then watch this video.
@@hunorportik5618 i know cpp and java . and also have a good understanding of ds and algo
thanks .
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.
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.
@MacHalaG thanks man. ☺️☺️
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
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
I'm pretty sure Django is the web framework getting all the flake here, lol.
18:10 Explicit is better than implicit
23:00
what are those parenthesis doing?!?!? 😂😂
Type hints and type checkers exist 🤦♂️
Not in old codebases tho
Ведущий умер в душе
python/django/mysql新教程:
python基础:ua-cam.com/video/g6RnSRDjd5M/v-deo.html
white background? seriously?
More readable
python code:
open("furby.py", mode="w").wright(data)
from furby import run
run()