Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the little knowledge of python?

Shulou Source: shulou.com Published: 2022-06-03 07:24:35 11月15日 Update

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.

Tags: Attributes data methods functions knowledge objects help clarity three content dynamic this articles novices right and wrong more industry information information channels puzzles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info Shulou Technology Redmi Linux