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

preview_player
Показать описание
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!

Рекомендации по теме
Комментарии
Автор

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.

htpcmagistrat
Автор

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.

younglife
Автор

I have to admit the comment section is more interesting and also more to my liking.

lazywarrior
Автор

The example made the code after refactoring more complex with all this backwards compatibility stuff.

warvariuc
Автор

Exellent talking on rewriting python code~

cnliving
Автор

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.

andreas_bergstrom
Автор

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.

TheWillRogers
Автор

Why don't you just use lru_cache?

HenockTesfaye
Автор

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.

aroundyouaroundme
Автор

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.

mmilerngruppe
visit shbcf.ru