Jack Diederich - HOWTO Write a Function - PyCon 2018

  Рет қаралды 68,199

PyCon 2018

PyCon 2018

Күн бұрын

Speaker: Jack Diederich
A function is a small chunk of code that does useful work. Your job when writing a function is to do it in a way that it easy to read. Based on over 15 years of code reviews here are some tips and guidelines I give again and again.
Slides can be found at: speakerdeck.com/pycon2018 and github.com/PyCon/2018-slides

Пікірлер: 34
@pratikjain4704
@pratikjain4704 4 жыл бұрын
1. Function length being 2 is great, but unnecessarily breaking it down isn't always useful, you have to think of readability too 2. Return from the function when the jobs done 3. In a func, define variables when you are about to use them rather than defining them all at the top 4. Use variable name as _ or ignored when you want to ignore a value 5. Use meaningful func/variable names 6. Explicit returns are better than implicit returns 7. Don't forcefully implement a new feature that you just learnt
@eukaryote0
@eukaryote0 3 жыл бұрын
Being stuck and not knowing what to do for 10 years after we stopped writing classes we finally know how to write some code
@edgeeffect
@edgeeffect 3 жыл бұрын
I'm a big big fan of Jack's "Stop Writing Classes" and it's great to find another talk up to the standard of that one... both these talks apply to the other languages as much as they do to Python too.
@nosknut
@nosknut 2 жыл бұрын
types are useful for code that will change, or code that other people will use later. I once came into a plain javascript rest API and i had no idea what data was passed around to what functions. I had to read nearly the entire codebase to safely make changes. Typescript gives you this information directly, as well as providing information about what will break if you make a change. Just change something, and click the compiler error links that show up in the console. Types aren't as much about the code that you write, as they are about the code you and others WILL write or change in the future when you have forgotten about it.
@dom1310df
@dom1310df 3 жыл бұрын
I don't know, some of those "good" examples are far harder for me to read.
@robbert-janmerk6783
@robbert-janmerk6783 5 жыл бұрын
At 20:18 The ignored parameter thing isn't really wise when you call functions with keyword arguments, as you will break the callers.
@sadhlife
@sadhlife 3 жыл бұрын
you can now make the first argument positional-only, don't know if it's convenient, but at least it won't be ambiguous
@GPHemsley
@GPHemsley 5 жыл бұрын
Ironically, this talk is like three times longer than it needs to be.
@dicai
@dicai 6 жыл бұрын
I didn't understand what he meant in one of the examples of "The gun in the mantle" bit @15:30 . def call_api(retries=MAX_RETRIES): Did he meant to we should avoid using variables for keyword arguments? If so we lose a bit of the semantics by adding a constant directly on the code, and I don't think that's a good practice for easier code review.
@tonyflury2291
@tonyflury2291 6 жыл бұрын
What he was saying is don't pre-define all of your variables up front - if you know 1/2 way down you have a loop which does a partial_sum of the input, don't define your sum variable at the top of the function. you should set your sum to zero literally just before the loop.
@patrickmullan8356
@patrickmullan8356 3 жыл бұрын
How do you avoid "if" in your code?
@-__--__aaaa
@-__--__aaaa 3 жыл бұрын
is polymorphism really replace "if" ?
@nosknut
@nosknut 2 жыл бұрын
overusing ternary expressions is one.
@gr3ut
@gr3ut 6 жыл бұрын
at 12:00, it can be a tad simpler. [config] = get_host_configs(matching=[name]) return config
@gromic-invest
@gromic-invest 6 жыл бұрын
and if there is 3 configs with the same name? you will all 3 configs, he say'd that if there a config with 2 same name we need to return error, because something go wrong
@gr3ut
@gr3ut 6 жыл бұрын
[config] = # does fail if you don't have exactly one element.
@gromic-invest
@gromic-invest 6 жыл бұрын
you are right, sorry i don't know about that, thats cool
@davidthomson140
@davidthomson140 6 жыл бұрын
If you wouldn't mind, could you break-down this solution for me? I'm relatively new and can't understand this.
@gr3ut
@gr3ut 6 жыл бұрын
it's called unpacking and there are many articles around on it, enjoy. treyhunner.com/2018/03/tuple-unpacking-improves-python-code-readability/
@tasoulman
@tasoulman 6 жыл бұрын
The github repo does not exist yet..
@MMphego
@MMphego 6 жыл бұрын
github.com/PyCon/pycon-archive
@tasoulman
@tasoulman 6 жыл бұрын
The slides link does not have any content, I get "PyCon 2018 hasn't published any talks." Are the slides in the GH repo ?
@twistedsim
@twistedsim 6 жыл бұрын
Typing is a great feature. You probably just don't understand why.
@gamen8209
@gamen8209 5 жыл бұрын
Please enlighten me?
@Nuttycomputer
@Nuttycomputer 4 жыл бұрын
Ga men not OP and I know this is a year late. Personally I like the new annotations for code completion in my editor. If I tell it what input type I am expecting for a variable I can quickly access methods for that type.
@sadhlife
@sadhlife 3 жыл бұрын
my man on the podium just said "I don't pass the wrong types in my functions", which made my head spin. That's like saying "I don't make mistakes". Type annotations make your code infinitely more readable, and it complements unit tests in a way that nothing else can. Here's a talk from the same pycon: kzbin.info/www/bejne/pn7Knn6Arrx-pJo
@dr.rodrigo554
@dr.rodrigo554 2 жыл бұрын
Who tf told him to give speeches? Give this guy a bed so he can sleep.
@TheInfirit
@TheInfirit 5 жыл бұрын
Job done, i got bored after 9 minutes and stopped watching.
Carl Meyer - Type-checked Python in the real world - PyCon 2018
32:10
Me: Don't cross there's cars coming
00:16
LOL
Рет қаралды 15 МЛН
I CAN’T BELIEVE I LOST 😱
00:46
Topper Guild
Рет қаралды 104 МЛН
🤔Какой Орган самый длинный ? #shorts
00:42
The child was abused by the clown#Short #Officer Rabbit #angel
00:55
兔子警官
Рет қаралды 25 МЛН
Reuven M. Lerner - Practical decorators - PyCon 2019
29:12
PyCon 2019
Рет қаралды 41 М.
When Booleans Are Not Enough... State Machines?
21:50
Next Day Video
Рет қаралды 24 М.
Machine Code Explained - Computerphile
20:32
Computerphile
Рет қаралды 110 М.
"What can't WebAssembly do?" - Katie Bell (PyCon AU 2023)
29:03
Me: Don't cross there's cars coming
00:16
LOL
Рет қаралды 15 МЛН