The COMPLETE guide to Python properties

preview_player
Показать описание
Properties are unbelievably useful in Python, and serve as a convenient shortcut to one of Python's most powerful concepts, and everyone's favourite thing, descriptors! (Don't worry, properties abstract all the complex stuff away.) We won't just be looking at what they do though, we'll be going into detail about how properties work, and why they're so powerful!

00:00 - Intro [1]
00:50 - Managing private attributes using getters, setters, and deleters [2]
05:04 - Computed properties
07:00 - Cached properties [3-4]
09:44 - How are properties implemented? [5-7]
14:58 - Outro



If you want to see more of me, join my Discord server!

I get a lot of people asking, so here's my setup!

Affiliations (I may earn a commission):



If you have any questions, don't hesitate to ask in the comments! I'll try and answer as soon as I can, providing someone else hasn't already done so.

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

Off the top of my head, one way to handle the issue you described with cached properties not responding to state change would be to handle the actual logic in a cached class method. The property would then pass the appropriate values to that method, meaning the call signature would change if any of the relevant attributes (or other variables) did. Of course, there would be some overhead to that, but I'm assuming the expensive stuff is things like disk reads, database queries, Internet requests, etc.

TheJamesM
Автор

Bro, I would be your first subscriber if I knew you're that kinda Coldplay fan to wear the Music of the Spheres tour t-shirt in a YouTube video lol, and great video :)

jimshapedcoding
Автор

14:07 But why? Why change the method names to _

johnmoff
Автор

so python's property is just this syntax in c#?

public class Test {
private int _age;

public int age {
get { return _age; }
set { _age = value; }
}

public Test(int age) {
_age = age;
}
}

Test test = new Test(42);
Console.WriteLine(test.age); // 42
test.age = 69;
Console.WriteLine(test.age); // 69

CripsyFries
visit shbcf.ru