Explain the descriptor class of Python magic method

Time:2021-12-4

Descriptor class requirements:

A descriptor is a property that assigns an instance of a particular type of class to another class

At least one of the following methods should be implemented:

?
1
2
3
4
5
6
7
8
•__get__(self, instance, owner)
– used to access a property, which returns the value of the property
 
•__set__(self, instance, value)
- it will be invoked in the attribute assignment operation without returning any content.
 
•__delete__(self, instance)
– control the deletion operation without returning anything

eg:

?
1
2
3
4
5
6
7
8
9
>>> class mydecriptor:
    def __get__(self,instance,owner):
        print("getting...",self,instance,owner)
    def __set__(self,instance,value):
        print("setting...",self,instance,value)
    def __delete__(self,instance):
        print("deleting...",self,instance)
>>> class test:
    x = mydecriptor()   #Take the instance of mydescriptor class and assign it to the property X of test class

Test:

Explain the descriptor class of Python magic method

eg:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
>>> class myproperty:
    def __init__(self,fget = none,fset = none,fdel = none):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel
    def __get__(self,instance,owner):
        return self.fget(instance)
    def __set__(self,instance,value):
        self.fset(instance,value)
    def __delete__(self,instance):
        self.fdel(instance)
 
        
>>> class c:
    def __init__(self):
        self._x = none
    def getx(self):
        return self._x
    def setx(self,value):
        self._x = value
    def delx(self):
        del self._x
    x = myproperty(getx,setx,delx)
 
    
>>> c = c()
>>> c.x = "hellow"
>>> c.x
'hellow'
>>> c._x
'hellow'
>>> del c.x
>>> c._x

eg:

Celsius to Fahrenheit: Fahrenheit = Celsius * 1.8 + 32

requirement:

• first define a temperature class, and then define two descriptor classes to describe the two attributes of Celsius and Fahrenheit.
• the two attributes are required to be automatically converted, that is, you can assign a value to the Celsius attribute, and then the printed Fahrenheit attribute is the result of automatic conversion.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
ss celsius:  #Celsius descriptor class
    def __init__(self,value = 26.0):#Self is an instance of the descriptor class itself (this is the Celsius descriptor class) (this is CEL)
        self.value = float(value)
    def __get__(self,instance,owner):#Instance is the instance of the class where the owner of this descriptor is located (this is temp)
        return self.value
    def __set__(self,instance,value):#Owner is the class itself where the owner of this descriptor is located (this is the temperature class)
        self.value = float(value)
 
class fahrenheit:   #Fahrenheit descriptor class
    def __get__(self,instance,owner):
        return instance.cel * 1.8 +32  #Celsius to Fahrenheit
    def __set__(self,instance,value):
        instance.cel = ((float)(value)- 32)/ 1.8
        ##Instance.cel will execute temperature.cel, and then execute the__ set__ method
        
class temperature:   #Temperature class
    cel = celsius()   #Set the Celsius attribute (the instance of the descriptor class is assigned to the attribute of the temperature class)
    fah = fahrenheit()#Temp.fah. When the attribute FAH is assigned, the Fahrenheit descriptor class will be executed__ set__ method
                        
>>> temp = temperature()
>>> temp.cel
26.0
>>> temp.fah
78.80000000000001
>>> temp.fah = 78.8
>>> temp.cel
25.999999999999996

This is the end of this article about explaining the descriptor class of Python magic methods. For more information about Python descriptor class, please search the previous articles of developepper or continue to browse the relevant articles below. I hope you will support developepper in the future!

Recommended Today

Hive built-in function summary

1. Related help operation functions View built-in functions: Show functions; Display function details: desc function ABS; Display function extension information: desc function extended concat; 2. Learn the ultimate mental method of built-in function Step 1: carefully read all the functions of the show functions command to establish an overall understanding and impression Step 2: use […]