Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the python property package

Shulou Source: shulou.com Published: 2022-06-01 05:16:21 09月29日 Update

This article mainly introduces how to use the python property package, which is very detailed and has a certain reference value. Friends who are interested must read it!

Property package

You can use attrs instead of data classes, and choose attrs for two reasons:

The version of Python used is higher than 3.7

Want more features

The Theattrs package supports all major Python versions, including CPython 2.7 and PyPy. Some attrs can provide supernormal data classes such as validators and converters. Take a look at some sample code:

Attrs classPerson (object): name = attrib (default='John') surname = attrib (default='Doe') age = attrib (init=False) p=Person () print (p) p=Person ('Bill' 'Gates') p.age=60 print (p) # Output: # Person (name='John', surname='Doe',age=NOTHING) # Person (name='Bill', surname='Gates', age=60)

In fact, the authors of attrs are already using PEP that introduces data classes. Data classes are deliberately kept simpler and easier to understand, while attrs provides all the features you might need.

The above is all the content of the article "how to use the python property package". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Data attributes content more version article two mainstream code value author interest function reason actual actually guys buddies features knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi vpn MariaDB Docker Redmi