What is the python __get__ method?

What can be achieved __get__that cannot be accomplished with the getter method on a native object?

I can think of a better separation of problems, but I think there are more.

+3
source share
2 answers

Used for descriptors. They are kind of like Python getters / seters and properties, but better. This is how Python implements the principle of uniform access. Python Descriptors

+5
source

Getter methods are ugly. This is much clearer:

obj.foobar

than

obj.get_foobar()

Secondly, it was used to implement the staticmethod, classmethod, and regular methods. All of them have slightly different behavior, and the method is __get__used to implement them.

+1
source

Source: https://habr.com/ru/post/1792795/


All Articles