I'm trying to understand what Python descriptors are and for that How useful can be However, I am failing it. I understand how they work, but this is my suspicion. Consider the following code:
square celsius (object): def __init __ (self, value = 0.0): self.value = float (value) def __get __ (self, example, owner ): Return Self.value def __set __ (self, example, value): self.value = float (value) square temperature (object): Celsius = Celsius ()
-
Why do I need a descriptor class? Please use this example or what you think is better.
-
What is the
example
andowner
here? (In__ go __
). So my question is, what is the purpose of the third parameter here? -
How do I call / use this example?
The descriptor is how the property of Python
Type is applied, a descriptor just applies __
, __ set __
, etc. and then its definition is added to the second category (as You did the temperature above the class). For example:
temporary = temperature () temp.celsius #calls.calca___get__
To access the property that you have given to the teller ( is assigned to Celsius)
in the above example) calls the appropriate descriptor method. Example of __ go __
class in the
example
(above, code> __ go __ will receive temp
, while owner
is square with pigment (hence it will be temperature
).
You need to understand the reasoning of using a descriptor class In this way, if a descriptor is used to cache some expensive operations, then it can store the value automatically and It can not be classed.
An article about a descriptor can be edited
As jchl said in the comment, if you simply Temperature.celsius
, example
will be none
.
Comments
Post a Comment