Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the little knowledge of python?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What are the small knowledge of python, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

1. Why write if _ _ name__ = ='_ _ main__':

Class User:def _ _ init__ (self): print ('test import') var_user = User ()

The runtime will print out

Import Test#test import

2. Property dynamic attribute to change the function into a property descriptor

# change the function into a property descriptor from datetime import date, datetimeclass User:def _ _ init__ (self, name, birthday): self.name = nameself.birthday = birthdayself._age = 0 @ property def age (self):''this method can only take values''. Year-self.birthday.year@age.setter def age (self Var_value):''setting value' 'self._age = var_valueif _ _ name__ =' _ _ main__': var_user = User ('tom', date (year=1985, month=2, day=6)) print (var_user.age) # 36 var_user.age = 100print (var_user._age) # 100

3. Attribute descriptor

The attribute descriptor can verify the properties of the object. The attribute descriptor involves three magic functions, one of which is the attribute descriptor, namely, _ _ get__, _ _ set__, and _ _ delete__,.

The _ _ get__ and _ _ set__ are called data descriptors, and the rest are called non-data attribute descriptors.

The order in which the property is called:

1: if var_age appears in _ _ dict__ of the class or its base class, and var_age is a data descriptor, _ _ get__ is called

If var_age appears in the _ _ dict__ of the object (obj), return obj__dict__ ['var_age'] directly.

3: if var_age appears in the _ _ dict__ of the class or base class, call the _ _ get__ method if the var_age is not a data descriptor, otherwise return _ _ dict__ ['var_age']

4: if the class has a _ _ getattr__ method, call the _ _ getattr__ method, otherwise throw an AttributeError

Import numbersclass IntField:def _ get__ (self, instance, owner): return self.valuedef _ set__ (self, instance, value): if not isinstance (value, numbers.Integral): raise ValueError ('int value') self.value = valuedef _ delete__ (self, instance): passclass NoDate:def _ get__ (self, instance) Owner): return self.valueclass TestUser: var_age = NoDate () class User: var_age = IntField () if _ _ name__ = ='_ main__': var_user = User () var_user.var_age = 10 print (var_user.__dict__) var_user.__dict__ ['var_age'] = 50 print (var_user.var_age) # 10 print (User.__dict _ _) var_user_test = TestUser () TestUser.var_age = 1 print (TestUser.var_age) # 1 var_user_test.var_age = 50 print (var_user_test.__dict__) var_user_test.__dict__ ['var_age'] = 100 print (var_user_test.var_age) # 100 print (TestUser.__dict__) is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report