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.
精彩评论