Refactoring Python Code: Profile Class

preview_player
Показать описание
In this Refactoring Code video we take a look at some Python code I saw on Twitter. We'll refactor a Profile class from Java-style code into Pythonic code.

When you're refactoring Python code something important to remember is that you're not only simplifying and reducing complexity, but often you'll want to completely rewrite some parts of the code to use Python features—such as the @property decorator.

-- Links --

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

I guess this a simplification of a real world approach with a proper database and all, but to me it makes no sense to have a class for something which clearly doesn’t require any OOP. One thing that I read in a very good software design book is that if in a class you only have 2 methods, and one is __init__(), then you don’t need it to be a class at all... 🙂

vvzen
Автор

I don't think that this code is now correct. With the property, you would actually call details.get(profile_id), when you access the property. Here it only calls it once when the constructor is called. So when details.get(profile_id) changes, your code would produce the same result. But with the property it would produce a different one. In case of an in-memory database I guess doing it with a property is the intended behaviour.

WatchingBlue
Автор

Really helpful! I have been thinking for a while about how to convert method to property. Thank you!

realxiao