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

How to use attrs to bid farewell to templates in Python

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

Share

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

How to use attrs to bid farewell to the model in 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 get something.

Learn more about solving PyPI problems in our series of articles covering seven Python libraries.

Python is one of the most popular programming languages today because it is open source, it has a wide range of uses (such as Web programming, business applications, games, scientific programming, etc.), and it is supported by a dynamic and dedicated community. This community is why we provide such a large and diverse package in Python Package Index (PyPI) to extend and improve Python. And solve the inevitable problems.

In this series, we will introduce seven PyPI libraries that can help you solve common Python problems. Today, we'll take a look at attrs, a Python package that helps you quickly write concise, correct code.

Attrs

If you've been writing Python for a while, you may be used to writing code like this:

Class Book (object): def _ _ init__ (self, isbn, name, author): self.isbn = isbn self.name = name self.author = author

Then write a _ _ repr__ function. Otherwise, it is difficult to record an instance of Book:

Def _ _ repr__ (self): return f "Book ({self.isbn}, {self.name}, {self.author})"

Next you will write a good-looking docstring to record the desired type. But you notice that you forgot to add the edition and published_year properties, so you have to modify them in five places.

What if you don't have to do this?

@ attr.s (auto_attribs=True) class Book (object): isbn: str name: str author: str published_year: int edition: int

Using the new type annotation syntax annotation type properties, attrs detects the comments and creates a class.

ISBN has a specific format. What if we want to force the format?

@ attr.s (auto_attribs=True) class Book (object): isbn: str = attr.ib () @ isbn.validator def pattern_match (self, attribute, value): M = re.match (r "^ (\ d {3} -)\ d {1value 3} -) if not m: raise ValueError (" incorrect format for isbn ") Value) name: str author: str published_year: int edition: int

The attrs library also supports immutable programming well. Changing the * * line to @ attr.s (auto_attribs=True, frozen=True) means that Book is now immutable: trying to modify a property will throw an exception. Instead, for example, if you want to release a year later, we can change it to attr.evolve (old_book, published_year=old_book.published_year+1) to get a new instance.

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