Brett Slatkin - Refactoring Python: Why and how to restructure your code - PyCon 2016

  Рет қаралды 63,871

PyCon 2016

PyCon 2016

8 жыл бұрын

Speaker: Brett Slatkin
As programs gain complexity, it becomes harder to add features and fix bugs. Reorganizing code is an effective way to make programs more manageable. This talk will show you Pythonic ways to do the most imporant ""refactorings"": Extract variables with __nonzero__; Change signatures with *args and **kwargs; Extract fields and classes with @property; Create stateful closures with __call__; and more!
Slides can be found at: speakerdeck.com/pycon2016 and github.com/PyCon/2016-slides

Пікірлер: 41
@tib7209
@tib7209 2 жыл бұрын
Don't focus too much on the solutions the presenter demonstrated but on the problems and he did a great job on that. I worked on a legacy project that suffered from the same problems demonstrated here and it was hell to refactor the code to a point to make it testable and easier to maintain as requirements grew larger. These are things every programmer should be aware of.
@diepmai6656
@diepmai6656 5 жыл бұрын
thank you Brett Slatkin, I have learned a lot from your book Effective python
@nguyenthanhdat93
@nguyenthanhdat93 6 жыл бұрын
Great tips! Thank you for sharing
@younglife88
@younglife88 7 жыл бұрын
i liked presentation, gives me more to learn and what others do... especially if you are learning, but not everything is or should become a class like others have said... but i think if your objects eventually become repeated code, classes will help out amazingly.
@HumanoidTyphoon91
@HumanoidTyphoon91 7 жыл бұрын
I found this to be a very interesting talk, even though I didn't like most of the final refactored code in the presentation. One thing I'd like to point out, though, is that the class (that IMHO really shouldn't be a class) didn't make testing better. You are testing internals, implementation details just for the sake of testing it. If you stopped one step before, would have been better (though again, at least I learned about the __bool__ function, so thanks for that).
@iktunutki
@iktunutki 8 жыл бұрын
I think the presentations you pointed to are really great! It's a bit strange that you'd mention them and then do a presentation that contains far more "write some classes" advice than I would consider idiomatic in Python. I found the advice about writing a class and defining a __bool__ really hacky, not pythonic and not "obvious" (not idiomatic will always imply "not obvious to the reader"). I'm also not convinced it's a good idea in terms of testing. You're testing the internals of the class, i.e. reimplementing the logic inside of the tests: why would I care that "November" is not an "ary" month, when all I want to know is if oysters are good to eat during that month?
@anandiyer5361
@anandiyer5361 4 жыл бұрын
Have a question. How is refactoring the class to include an animal as part of its __init__, superior to inheritance?
@lawrencerosen7113
@lawrencerosen7113 5 жыл бұрын
I never saw Python 2, please give some idea how These things are best done in the Pythob-3 context
@cnliving
@cnliving 7 жыл бұрын
Exellent talking on rewriting python code~
@jiansenxmu
@jiansenxmu 7 жыл бұрын
Thanks!
@linpershey
@linpershey 2 жыл бұрын
great talk!
@htpcmagistrat3535
@htpcmagistrat3535 6 жыл бұрын
Good talk @brettorama , though I agree with other commenters that refactoring from functions to classes is probably a bad idea and a Java-ism that does not belong in Python land. You linked to the talk that people should stop using classes, so I will forgive you for that, though :) I have a comment on the tombstones you suggest at the end of your talk; it is not the *only* way to safeguard against setting pet.age = 5. Rather than sprinkle your code with superfluous tombstone properties, you could just add __slots__ = 'name', 'animal' to the Pet class.
@warvariuc
@warvariuc 8 жыл бұрын
The example made the code after refactoring more complex with all this backwards compatibility stuff.
@BrettSlatkin
@BrettSlatkin 8 жыл бұрын
That's only a temporary state. The backwards compatibility code lets you move everything over to the new and simpler usage. Then you remove all of the backwards compatibility and have a simpler design overall.
@warvariuc
@warvariuc 8 жыл бұрын
Why not changing all the code at once, including the callers?
@BrettSlatkin
@BrettSlatkin 8 жыл бұрын
There are a bunch of reasons. If multiple people are working on the same codebase at the same time, you can get merge conflicts if you try to change everything all at the same time. When you're working alone, by doing backwards compatibility you have more of a chance to incrementally test each phase of a big change, instead of just crossing your fingers that it'll all work at the end.
@warvariuc
@warvariuc 8 жыл бұрын
I don't know, I doubt. Making such non-trivial backwards compatible changes makes the code more complex -- more probability for errors. But if you have tests, changing all the the code at once can more robust. While I agree that for a public API this can be a good approach. Anyway thanks for the talk.
@BrettSlatkin
@BrettSlatkin 8 жыл бұрын
Ah sorry. To be clear: I agree that this is overkill for many cases. There are other refactorings that you just do all at once. I wanted to show what it takes when things are more complex.
@TheWillRogers
@TheWillRogers 5 жыл бұрын
This is awesome. Really helped. But the age belongs to the pet. Gila does not have age like it does scales, but Geraldo the Gila does have an age.
@lazywarrior
@lazywarrior 2 жыл бұрын
I have to admit the comment section is more interesting and also more to my liking.
@shaarg
@shaarg 5 жыл бұрын
Glitch in Matrix
@Yqhs
@Yqhs 7 жыл бұрын
If we are talking about readability he sholdnt use .format() ?
@younglife88
@younglife88 7 жыл бұрын
i think the same , looks cleaner than %s
@tandavme
@tandavme 6 жыл бұрын
f-strings bro))
@stanleychan3212
@stanleychan3212 4 жыл бұрын
Sorry, but I don't think writing OystersGood class and TomatoesGood class is good design when simple functions suffices. Plus I find it hard to treat OystersGood(month=1) as a tangible object.
@Andrumen01
@Andrumen01 2 жыл бұрын
There is a small caveat to refactoring that is hardly mentioned: If refactoring MUST break the old functions, version! Refactoring, most times, leads to a completely new version. This way, you don't lose (a lot of) backward compatibility and can move forward to better software.
@princepritam71
@princepritam71 3 жыл бұрын
Portland Oregon definitely reminds me of DAYS GONE 🤠
@HenockTesfaye
@HenockTesfaye 7 жыл бұрын
Why don't you just use lru_cache?
@one-anachronism
@one-anachronism 3 жыл бұрын
By the end of the talk .... The only word that came to my mind was, OVERKILL
@el_chivo99
@el_chivo99 3 жыл бұрын
yeah it kind of is. however these are great tips when working on a huge codebase. it’s hard to convey that in a short talk
@andreas_bergstrom
@andreas_bergstrom 7 жыл бұрын
Can't we all just stop using these nonsense code examples with animals, cars, etc.? Would be way easier to follow along if people instead used real world scenarios such as a game, webshop, discussion board or what not.
@move1649
@move1649 6 жыл бұрын
+1
@tectubedk
@tectubedk 5 жыл бұрын
The reason for using animals and cars is because it's generic. If you use real world code you would spend more time explaining the code witch is a waste of time.
@gJonii
@gJonii 4 жыл бұрын
@@tectubedk If presenter put more thought to it, they should be able to come up with examples that are closer to their audiences everyday experiences. Even if you sacrifice a bit in intuitive understanding and have to explain the example a bit, I often find it to be really really interesting to see an actual code example where some idea just discussed has been used to solve a real problem. In rapid fire talks which just give ideas one after another, it might be hard for presenter to do this, so I don't mind if someone just goes the easy route and uses person class or something to give really simplistic example, but I certainly do prefer one over the other if given a choice.
@petrockspiracy3120
@petrockspiracy3120 4 жыл бұрын
I don't have tests. I figured tests was more of an advanced topic than refactoring but I guess not? :P
@aroundyouaroundme
@aroundyouaroundme 8 жыл бұрын
Omg. I've stopped watching at 12 min when functions bacame classes. Dude, never ever design your code this way. You should never have facilities that exist only because of tests.
@dengan699
@dengan699 7 жыл бұрын
hello, could you please elaborate? I always thought it is better to refactore code so that it is more testable (but I agree on the fact that theses classes are weird)
@AlqGo
@AlqGo 6 жыл бұрын
I don't think it's that weird. It's actually very cool, because by converting the function into a class, you can suddenly explicitly test the internal state that you don't have direct access to with the function. For example, in his example, he could explicitly test for "r" and "ary" just by accessing them directly as attributes of the class, which was impossible for him to do with the original function.
@alexd7466
@alexd7466 Жыл бұрын
Martin Fowler fanboys should go back to Java and stop messing up good Python code with their practices that only make sense in Java. They already destroyed PHP years ago, and now found a new target.
@mmilerngruppe
@mmilerngruppe 4 жыл бұрын
oh boy, the one thing I learned by this video is that you should write things right on first attempt. refactoring is long boring stuff you better don't do.
Guido van Rossum - Python Language - PyCon 2016
42:14
PyCon 2016
Рет қаралды 138 М.
Как бесплатно замутить iphone 15 pro max
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 8 МЛН
НЫСАНА КОНЦЕРТ 2024
2:26:34
Нысана театры
Рет қаралды 1 МЛН
Structural Pattern Matching in the Real World - Raymond Hettinger
32:55
Jake Vanderplas - Statistics for Hackers - PyCon 2016.mp4
40:45
PyCon 2016
Рет қаралды 82 М.
Transforming Code into Beautiful, Idiomatic Python
48:51
Next Day Video
Рет қаралды 1 МЛН
Raymond Hettinger - Super considered super! - PyCon 2015
46:52
PyCon 2015
Рет қаралды 122 М.
Ned Batchelder: Getting Started Testing - PyCon 2014
42:44
PyCon 2014
Рет қаралды 57 М.
Как бесплатно замутить iphone 15 pro max
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 8 МЛН