Warum sollten Sie Attrs häufiger verwenden?

WBOY
Freigeben: 2024-08-21 06:14:33
Original
760 Leute haben es durchsucht

Why should you use attrs more

Einführung

Pythons attrs-Bibliothek ist ein Game-Changer für Entwickler, die die Klassenerstellung vereinfachen und den Boilerplate-Code reduzieren möchten. Dieser Bibliothek vertraut sogar die NASA.
Attrs wurde 2015 von Hynek Schlawack entwickelt und hat sich aufgrund seiner Fähigkeit, automatisch spezielle Methoden zu generieren und eine saubere, deklarative Möglichkeit zum Definieren von Klassen bereitzustellen, schnell zu einem beliebten Tool unter Python-Entwicklern entwickelt.
Datenklassen sind eine Art Teilmenge von Attributen.

Warum Attrs nützlich sind:

  • Reduziert den Boilerplate-Code
  • Verbessert die Lesbarkeit und Wartbarkeit des Codes
  • Bietet leistungsstarke Funktionen zur Datenvalidierung und -konvertierung
  • Verbessert die Leistung durch optimierte Implementierungen

2. Erste Schritte mit Attrs

Installation:
Um mit attrs zu beginnen, können Sie es mit pip:
installieren

pip install attrs
Nach dem Login kopieren

Grundlegende Verwendung:
Hier ist ein einfaches Beispiel für die Verwendung von Attrs zum Definieren einer Klasse:

import attr

@attr.s
class Person:
    name = attr.ib()
    age = attr.ib()

# Creating an instance
person = Person("Alice", 30)
print(person)  # Person(name='Alice', age=30)
Nach dem Login kopieren

3. Kernfunktionen von attrs

A. Automatische Methodengenerierung:

attrs generiert automatisch die Methoden init, repr und eq für Ihre Klassen:

@attr.s
class Book:
    title = attr.ib()
    author = attr.ib()
    year = attr.ib()

book1 = Book("1984", "George Orwell", 1949)
book2 = Book("1984", "George Orwell", 1949)

print(book1)  # Book(title='1984', author='George Orwell', year=1949)
print(book1 == book2)  # True
Nach dem Login kopieren

B. Attributdefinition mit Typen und Standardwerten:

import attr
from typing import List

@attr.s
class Library:
    name = attr.ib(type=str)
    books = attr.ib(type=List[str], default=attr.Factory(list))
    capacity = attr.ib(type=int, default=1000)

library = Library("City Library")
print(library)  # Library(name='City Library', books=[], capacity=1000)
Nach dem Login kopieren

C. Validatoren und Konverter:

import attr

def must_be_positive(instance, attribute, value):
    if value <= 0:
        raise ValueError("Value must be positive")

@attr.s
class Product:
    name = attr.ib()
    price = attr.ib(converter=float, validator=[attr.validators.instance_of(float), must_be_positive])

product = Product("Book", "29.99")
print(product)  # Product(name='Book', price=29.99)

try:
    Product("Invalid", -10)
except ValueError as e:
    print(e)  # Value must be positive
Nach dem Login kopieren

4. Erweiterte Nutzung

A. Anpassen des Attributverhaltens:

import attr

@attr.s
class User:
    username = attr.ib()
    _password = attr.ib(repr=False)  # Exclude from repr

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, value):
        self._password = hash(value)  # Simple hashing for demonstration

user = User("alice", "secret123")
print(user)  # User(username='alice')
Nach dem Login kopieren

B. Eingefrorene Instanzen und Slots:

@attr.s(frozen=True) # slots=True is the default
class Point:
    x = attr.ib()
    y = attr.ib()

point = Point(1, 2)
try:
    point.x = 3  # This will raise an AttributeError
except AttributeError as e:
    print(e)  # can't set attribute
Nach dem Login kopieren

C. Werksfunktionen und Post-Init-Verarbeitung:

import attr
import uuid

@attr.s
class Order:
    id = attr.ib(factory=uuid.uuid4)
    items = attr.ib(factory=list)
    total = attr.ib(init=False)

    def __attrs_post_init__(self):
        self.total = sum(item.price for item in self.items)

@attr.s
class Item:
    name = attr.ib()
    price = attr.ib(type=float)

order = Order(items=[Item("Book", 10.99), Item("Pen", 1.99)])
print(order)  # Order(id=UUID('...'), items=[Item(name='Book', price=10.99), Item(name='Pen', price=1.99)], total=12.98)
Nach dem Login kopieren

5. Best Practices und häufige Fallstricke

Best Practices:

  • Verwenden Sie Typanmerkungen für eine bessere Lesbarkeit des Codes und IDE-Unterstützung
  • Nutzen Sie Validatoren für die Datenintegrität
  • Verwenden Sie eingefrorene Klassen für unveränderliche Objekte
  • Nutzen Sie die Vorteile der automatischen Methodengenerierung, um die Codeduplizierung zu reduzieren

Häufige Fallstricke:

  • Vergessen, @attr.s Decorator für die Klasse zu verwenden
  • Übermäßiger Einsatz komplexer Validatoren, die separate Methoden sein könnten
  • Ohne Berücksichtigung der Leistungsauswirkungen einer umfassenden Nutzung von Werksfunktionen

6. attrs vs. andere Bibliotheken

Library Features Performance Community
attrs Automatic method generation, attribute definition with types and default values, validators and converters Better performance than manual code Active community
pydantic Data validation and settings management, automatic method generation, attribute definition with types and default values, validators and converters Good performance Active community
dataclasses Built into Python 3.7+, making them more accessible Tied to the Python version Built-in Python library

attrs and dataclasses are faster than pydantic1.

Comparison with dataclasses:

  • attrs is more feature-rich and flexible
  • dataclasses are built into Python 3.7+, making them more accessible
  • attrs has better performance in most cases
  • dataclasses are tied to the Python version, while attrs as an external library can be used with any Python version.

Comparison with pydantic:

  • pydantic is focused on data validation and settings management
  • attrs is more general-purpose and integrates better with existing codebases
  • pydantic has built-in JSON serialization, while attrs requires additional libraries

When to choose attrs:

  • For complex class hierarchies with custom behaviors
  • When you need fine-grained control over attribute definitions
  • For projects that require Python 2 compatibility (though less relevant now)

7. Performance and Real-world Applications

Performance:
attrs generally offers better performance than manually written classes or other libraries due to its optimized implementations.

Real-world example:

from attr import define, Factory
from typing import List, Optional

@define
class Customer:
    id: int
    name: str
    email: str
    orders: List['Order'] = Factory(list)

@define
class Order:
    id: int
    customer_id: int
    total: float
    items: List['OrderItem'] = Factory(list)

@define
class OrderItem:
    id: int
    order_id: int
    product_id: int
    quantity: int
    price: float

@define
class Product:
    id: int
    name: str
    price: float
    description: Optional[str] = None

# Usage
customer = Customer(1, "Alice", "alice@example.com")
product = Product(1, "Book", 29.99, "A great book")
order_item = OrderItem(1, 1, 1, 2, product.price)
order = Order(1, customer.id, 59.98, [order_item])
customer.orders.append(order)

print(customer)
Nach dem Login kopieren

8. Conclusion and Call to Action

attrs is a powerful library that simplifies Python class definitions while providing robust features for data validation and manipulation. Its ability to reduce boilerplate code, improve readability, and enhance performance makes it an invaluable tool for Python developers.

Community resources:

  • GitHub repository: https://github.com/python-attrs/attrs
  • Documentation: https://www.attrs.org/
  • PyPI page: https://pypi.org/project/attrs/

Try attrs in your next project and experience its benefits firsthand. Share your experiences with the community and contribute to its ongoing development. Happy coding!


  1. https://stefan.sofa-rockers.org/2020/05/29/attrs-dataclasses-pydantic/ ↩

Das obige ist der detaillierte Inhalt vonWarum sollten Sie Attrs häufiger verwenden?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:dev.to
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!