What’s New In Python 3.11

One of the features that we like most is:

EP 681: Data Class Transforms

dataclass_transform may be used to decorate a class, metaclass, or a function that is itself a decorator. The presence of @dataclass_transform() tells a static type checker that the decorated object performs runtime “magic” that transforms a class, giving it dataclasses.dataclass()-like behaviors.

For example:

# The create_model decorator is defined by a library.
def create_model(cls: Type[T]) -> Type[T]:
    cls.__init__ = ...
    cls.__eq__ = ...
    cls.__ne__ = ...
    return cls

# The create_model decorator can now be used to create new model
# classes, like this:
class CustomerModel:
    id: int
    name: str

c = CustomerModel(id=327, name="John Smith")
Full details are at: https://docs.python.org/3.11/whatsnew/3.11.html


No responses yet

Leave a Reply

Your email address will not be published.

Latest Comments