In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "Python how to use descriptors to achieve attribute type checking", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Python how to use descriptors to achieve attribute type checking" article.
1. How to use descriptors to do type checking on instance properties? Actual case:
In a project, we implemented some classes and wanted to do type checking on their instance properties like statically typed languages.
P = Person () p.name = 'Bob' # name attribute must be str p.age = 18 # Age must be int p.height = 1.83 # height must be float
Requirements: (1) you can specify a type for the instance variable name
(2) throw an exception when given an incorrect type
Solution:
Use descriptors to implement properties that require type checking: implement the _ _ get__, _ _ set__,__delete__ method respectively, and use the isinstance function within _ _ set__ to do type checking.
Extension: statically typed language variables can refer to only one object of a certain type and cannot be changed. Type checking is completed by the compiler in the compilation phase. For Python dynamically typed language, a variable can refer to any type of object and can be changed in real time, that is, the interpreter can not complete type checking and can only implement it on its own.
What is a descriptor? A descriptor is a class that contains methods such as _ _ get__, _ _ set__,__delete__. As long as these three methods contain one of them, it is a descriptor.
An instance property is to use an instance of another class as a numerical property of that class.
2. Code demonstration
(1) introduction of descriptor definition and access process
Class Descriptor (object): def _ get__ (self, instance, cls): # instance is used to distinguish between using classes to access x or using instances to access x print ('in _ _ get__', instance, cls) return instance.__dict__ ['x'] def _ set__ (self, instance Value): # check for types in set print ('in _ set__') instance.__dict__ ['x'] = value def _ _ delete__ (self Instance): print ('in _ del__') class A (object): # define a class attribute in the class x x = Descriptor () a = A () # will be intercepted by the _ _ get__ method of Descriptor print (a.x) # access class attributes directly using class A. Instance will be passed into Noneprint (A.x) # will be intercepted by the _ _ set__ method of Descriptor A. x = intercepted by the _ _ del__ method of Descriptor. Generally speaking, the dictionary of instance.__dict__ is accessed among the descriptors. That is, to operate on its true properties.'' A = A () a.x = 5print (a.
(2) implement to check instance property types using descriptors
Class Attr (object): def _ init__ (self, name, type_): self.name = name self.type_ = type_ def _ get__ (self, instance, cls): return instance.__dict__ [self.name] def _ set__ (self, instance, value): # detect if not isinstance (value) for field types Self.type_): raise TypeError ('expected an% s'% self.type_) instance.__dict__ [self.name] = value def _ _ delete__ (self, instance): del instance.__dict__ [self.name] class Person (object): # define a name field Apply for descriptor instance name = Attr ('name', str) age = Attr (' age', int) height = Attr ('height', float) p = Person () p.name =' Bob'print (p.name) # age assignment string type throw exception error # p.age = '17' is about how Python uses descriptors to implement attribute type checking. I believe you all have some understanding. I hope the content shared by the editor will be helpful to all of you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.