SHARE
TWEET

Untitled

a guest Sep 19th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def __get__(self,instance,owner):
  2.     pass
  3. def __set__(self,instance,value):
  4.     pass
  5. def __delete__(self,instance):
  6.     pass
  7.    
  8. #DEMOS
  9. class Int_validation:
  10.    
  11.     def __get__(self, instance, owner):
  12.         return  self.value #owner:<class '__main__.Student'> instance:<__main__.Student object at 0x107e891d0>
  13.    
  14.     def __set__(self, instance, value):
  15.         if  isinstance(value,int) and 0<value<100:
  16.             self.value=value
  17.         else:
  18.             print("请输入合法的数字")
  19.     def __delete__(self, instance):
  20.         pass
  21.  
  22. class Student:
  23.     age=9
  24.     #创建一个实例作为另一个类的属性:
  25.     age = Int_validation() #<__main__.Int_validation object at 0x1096999b0>
  26.  
  27. stu=Student()    #<__main__.Student object at 0x107e891d0>
  28. stu.age=50    #覆盖类属性9
  29. print(stu.age) # 50
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top