reshefm
reshefm

Reputation: 6177

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 of concerns but I guess there is more.

Upvotes: 4

Views: 1051

Answers (2)

Winston Ewert
Winston Ewert

Reputation: 45059

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.

Upvotes: 1

Y.H Wong
Y.H Wong

Reputation: 7244

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

Upvotes: 5

Related Questions