开发者

What is python __get__ method good for?

开发者 https://www.devze.com 2023-02-10 21:03 出处:网络
What can __get__ achieve that can\'t be done with with a getter method on the owning object? I can think of better separation开发者_JS百科 of concerns but I guess there is more.It\'s used for descrip

What can __get__ achieve that can't be done with with a getter method on the owning object?

I can think of better separation开发者_JS百科 of concerns but I guess there is more.


It's used for descriptors. They are kind of like Python's getters/setters, and properties, but better. It's how Python implements the Uniform Access Principle. Python Descriptors


Getter methods are ugly. It is much clearer to do:

obj.foobar

than

obj.get_foobar()

Secondly, it used to implement staticmethod, classmethod, and regular methods. All of these have slightly different behaviors and the __get__ method is used to implement them.

0

精彩评论

暂无评论...
验证码 换一张
取 消