开发者

What is python __get__ method good for?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜