In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how Python uses descriptors. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
Descriptors
Descriptors determine how object properties are accessed. Descriptors are used to customize what happens when you want to reference an attribute.
The way to build a descriptor is to define at least one of the following three methods. Note that instance in the following is the object instance containing the accessed attribute, while owner is the class that is rhetorical by the descriptor.
get(self, instance, owner) -This method is called when the attribute is obtained by (value = obj.attr), and the return value of this method is assigned to the part of the code that requested the attribute value. set(self, instance, value) -This method is called when you want to set the value of an attribute (obj.attr ='value '), it does not return any value. delete(self, instance) -Called when an attribute is deleted from an object (del obj.attr). For an understanding of instance and owner, consider the following code:
class Celsius(object): def __init__(self, value=0.0): self.value = float(value) def __get__(self, instance, owner): return self.value def __set__(self, instance, value): self.value = float(value)class Temperature(object): celsius = Celsius()temp=Temperature()temp.celsius #calls Celsius.__ Get__Thank you for reading! About "python how to use descriptor" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.