PEP 557 – Data Classes
- Author:
- Eric V. Smith <eric at trueblade.com>
- Status:
- Final
- Type:
- Standards Track
- Created:
- 02-Jun-2017
- Python-Version:
- 3.7
- Post-History:
- 08-Sep-2017, 25-Nov-2017, 30-Nov-2017, 01-Dec-2017, 02-Dec-2017, 06-Jan-2018, 04-Mar-2018
- Resolution:
- Python-Dev message
Notice for Reviewers
This PEP and the initial implementation were drafted in a separate repo:https://github.com/ericvsmith/dataclasses.Before commenting in a public forum please at least read thediscussionlisted at the end of this PEP.
Abstract
This PEP describes an addition to the standard library called Data Classes. Although they use a very different mechanism, Data Classes can be thought of as “mutable namedtuples with defaults”. Because Data Classes use normal class definition syntax, you are free to use inheritance, metaclasses, docstrings, user-defined methods, class factories, and other Python class features.
A class decorator is provided which inspects a class definition for variables with type annotations as defined inPEP 526,“Syntax for Variable Annotations”. In this document, such variables are called fields. Using these fields, the decorator adds generated method definitions to the class to support instance initialization, a repr, comparison methods, and optionally other methods as described in the Specificationsection. Such a class is called a Data Class, but there’s really nothing special about the class: the decorator adds generated methods to the class and returns the same class it was given.
As an example:
@dataclass
classInventoryItem:
'''Class for keeping track of an item in inventory.'''
name:str
unit_price:float
quantity_on_hand:int=0
deftotal_cost(self)->float:
returnself.unit_price*self.quantity_on_hand
The@dataclass
decorator will add the equivalent of these methods
to the InventoryItem class:
def__init__(self,name:str,unit_price:float,quantity_on_hand:int=0)->None:
self.name=name
self.unit_price=unit_price
self.quantity_on_hand=quantity_on_hand
def__repr__(self):
returnf'InventoryItem(name={self.name!r},unit_price={self.unit_price!r},quantity_on_hand={self.quantity_on_hand!r})'
def__eq__(self,other):
ifother.__class__isself.__class__:
return(self.name,self.unit_price,self.quantity_on_hand)==(other.name,other.unit_price,other.quantity_on_hand)
returnNotImplemented
def__ne__(self,other):
ifother.__class__isself.__class__:
return(self.name,self.unit_price,self.quantity_on_hand)!=(other.name,other.unit_price,other.quantity_on_hand)
returnNotImplemented
def__lt__(self,other):
ifother.__class__isself.__class__:
return(self.name,self.unit_price,self.quantity_on_hand)<(other.name,other.unit_price,other.quantity_on_hand)
returnNotImplemented
def__le__(self,other):
ifother.__class__isself.__class__:
return(self.name,self.unit_price,self.quantity_on_hand)<=(other.name,other.unit_price,other.quantity_on_hand)
returnNotImplemented
def__gt__(self,other):
ifother.__class__isself.__class__:
return(self.name,self.unit_price,self.quantity_on_hand)>(other.name,other.unit_price,other.quantity_on_hand)
returnNotImplemented
def__ge__(self,other):
ifother.__class__isself.__class__:
return(self.name,self.unit_price,self.quantity_on_hand)>=(other.name,other.unit_price,other.quantity_on_hand)
returnNotImplemented
Data Classes save you from writing and maintaining these methods.
Rationale
There have been numerous attempts to define classes which exist primarily to store values which are accessible by attribute lookup. Some examples include:
- collections.namedtuple in the standard library.
- typing.NamedTuple in the standard library.
- The popular attrs[1]project.
- George Sakkis’ recordType recipe[2],a mutable data type inspired by collections.namedtuple.
- Many example online recipes[3],packages[4],and questions[5]. David Beazley used a form of data classes as the motivating example in a PyCon 2013 metaclass talk[6].
So, why is this PEP needed?
With the addition ofPEP 526,Python has a concise way to specify the type of class members. This PEP leverages that syntax to provide a simple, unobtrusive way to describe Data Classes. With two exceptions, the specified attribute type annotation is completely ignored by Data Classes.
No base classes or metaclasses are used by Data Classes. Users of these classes are free to use inheritance and metaclasses without any interference from Data Classes. The decorated classes are truly “normal” Python classes. The Data Class decorator should not interfere with any usage of the class.
One main design goal of Data Classes is to support static type
checkers. The use ofPEP 526syntax is one example of this, but so is
the design of thefields()
function and the@dataclass
decorator. Due to their very dynamic nature, some of the libraries
mentioned above are difficult to use with static type checkers.
Data Classes are not, and are not intended to be, a replacement mechanism for all of the above libraries. But being in the standard library will allow many of the simpler use cases to instead leverage Data Classes. Many of the libraries listed have different feature sets, and will of course continue to exist and prosper.
Where is it not appropriate to use Data Classes?
- API compatibility with tuples or dicts is required.
- Type validation beyond that provided by PEPs 484 and 526 is required, or value validation or conversion is required.
Specification
All of the functions described in this PEP will live in a module named
dataclasses
.
A functiondataclass
which is typically used as a class decorator
is provided to post-process classes and add generated methods,
described below.
Thedataclass
decorator examines the class to findfield
s. A
field
is defined as any variable identified in
__annotations__
.That is, a variable that has a type annotation.
With two exceptions described below, none of the Data Class machinery
examines the type specified in the annotation.
Note that__annotations__
is guaranteed to be an ordered mapping,
in class declaration order. The order of the fields in all of the
generated methods is the order in which they appear in the class.
Thedataclass
decorator will add various “dunder” methods to the
class, described below. If any of the added methods already exist on the
class, aTypeError
will be raised. The decorator returns the same
class that is called on: no new class is created.
Thedataclass
decorator is typically used with no parameters and
no parentheses. However, it also supports the following logical
signature:
defdataclass(*,init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False)
Ifdataclass
is used just as a simple decorator with no
parameters, it acts as if it has the default values documented in this
signature. That is, these three uses of@dataclass
are equivalent:
@dataclass
classC:
...
@dataclass()
classC:
...
@dataclass(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False)
classC:
...
The parameters todataclass
are:
init
:If true (the default), a__init__
method will be generated.repr
:If true (the default), a__repr__
method will be generated. The generated repr string will have the class name and the name and repr of each field, in the order they are defined in the class. Fields that are marked as being excluded from the repr are not included. For example:InventoryItem(name='widget',unit_price=3.0,quantity_on_hand=10)
.If the class already defines
__repr__
,this parameter is ignored.eq
:If true (the default), an__eq__
method will be generated. This method compares the class as if it were a tuple of its fields, in order. Both instances in the comparison must be of the identical type.If the class already defines
__eq__
,this parameter is ignored.order
:If true (the default is False),__lt__
,__le__
,__gt__
,and__ge__
methods will be generated. These compare the class as if it were a tuple of its fields, in order. Both instances in the comparison must be of the identical type. Iforder
is true andeq
is false, aValueError
is raised.If the class already defines any of
__lt__
,__le__
,__gt__
,or__ge__
,thenValueError
is raised.unsafe_hash
:IfFalse
(the default), the__hash__
method is generated according to howeq
andfrozen
are set.If
eq
andfrozen
are both true, Data Classes will generate a__hash__
method for you. Ifeq
is true andfrozen
is false,__hash__
will be set toNone
,marking it unhashable (which it is). Ifeq
is false,__hash__
will be left untouched meaning the__hash__
method of the superclass will be used (if the superclass isobject
,this means it will fall back to id-based hashing).Although not recommended, you can force Data Classes to create a
__hash__
method withunsafe_hash=True
.This might be the case if your class is logically immutable but can nonetheless be mutated. This is a specialized use case and should be considered carefully.If a class already has an explicitly defined
__hash__
the behavior when adding__hash__
is modified. An explicitly defined__hash__
is defined when:__eq__
is defined in the class and__hash__
is defined with any value other thanNone
.__eq__
is defined in the class and any non-None
__hash__
is defined.__eq__
is not defined on the class, and any__hash__
is defined.
If
unsafe_hash
is true and an explicitly defined__hash__
is present, thenValueError
is raised.If
unsafe_hash
is false and an explicitly defined__hash__
is present, then no__hash__
is added.See the Python documentation[7]for more information.
frozen
:If true (the default is False), assigning to fields will generate an exception. This emulates read-only frozen instances. If either__getattr__
or__setattr__
is defined in the class, thenValueError
is raised. See the discussion below.
field
s may optionally specify a default value, using normal
Python syntax:
@dataclass
classC:
a:int# 'a' has no default value
b:int=0# assign a default value for 'b'
In this example, botha
andb
will be included in the added
__init__
method, which will be defined as:
def__init__(self,a:int,b:int=0):
TypeError
will be raised if a field without a default value
follows a field with a default value. This is true either when this
occurs in a single class, or as a result of class inheritance.
For common and simple use cases, no other functionality is required.
There are, however, some Data Class features that require additional
per-field information. To satisfy this need for additional
information, you can replace the default field value with a call to
the providedfield()
function. The signature offield()
is:
deffield(*,default=MISSING,default_factory=MISSING,repr=True,
hash=None,init=True,compare=True,metadata=None)
TheMISSING
value is a sentinel object used to detect if the
default
anddefault_factory
parameters are provided. This
sentinel is used becauseNone
is a valid value fordefault
.
The parameters tofield()
are:
default
:If provided, this will be the default value for this field. This is needed because thefield
call itself replaces the normal position of the default value.default_factory
:If provided, it must be a zero-argument callable that will be called when a default value is needed for this field. Among other purposes, this can be used to specify fields with mutable default values, as discussed below. It is an error to specify bothdefault
anddefault_factory
.init
:If true (the default), this field is included as a parameter to the generated__init__
method.repr
:If true (the default), this field is included in the string returned by the generated__repr__
method.compare
:If True (the default), this field is included in the generated equality and comparison methods (__eq__
,__gt__
, et al.).hash
:This can be a bool orNone
.If True, this field is included in the generated__hash__
method. IfNone
(the default), use the value ofcompare
:this would normally be the expected behavior. A field should be considered in the hash if it’s used for comparisons. Setting this value to anything other thanNone
is discouraged.One possible reason to set
hash=False
butcompare=True
would be if a field is expensive to compute a hash value for, that field is needed for equality testing, and there are other fields that contribute to the type’s hash value. Even if a field is excluded from the hash, it will still be used for comparisons.metadata
:This can be a mapping or None. None is treated as an empty dict. This value is wrapped intypes.MappingProxyType
to make it read-only, and exposed on the Field object. It is not used at all by Data Classes, and is provided as a third-party extension mechanism. Multiple third-parties can each have their own key, to use as a namespace in the metadata.
If the default value of a field is specified by a call tofield()
,
then the class attribute for this field will be replaced by the
specifieddefault
value. If nodefault
is provided, then the
class attribute will be deleted. The intent is that after the
dataclass
decorator runs, the class attributes will all contain
the default values for the fields, just as if the default value itself
were specified. For example, after:
@dataclass
classC:
x:int
y:int=field(repr=False)
z:int=field(repr=False,default=10)
t:int=20
The class attributeC.z
will be10
,the class attribute
C.t
will be20
,and the class attributesC.x
andC.y
will not be set.
Field
objects
Field
objects describe each defined field. These objects are
created internally, and are returned by thefields()
module-level
method (see below). Users should never instantiate aField
object directly. Its documented attributes are:
name
:The name of the field.type
:The type of the field.default
,default_factory
,init
,repr
,hash
,compare
,andmetadata
have the identical meaning and values as they do in thefield()
declaration.
Other attributes may exist, but they are private and must not be inspected or relied on.
post-init processing
The generated__init__
code will call a method named
__post_init__
,if it is defined on the class. It will be called
asself.__post_init__()
.If no__init__
method is generated,
then__post_init__
will not automatically be called.
Among other uses, this allows for initializing field values that depend on one or more other fields. For example:
@dataclass
classC:
a:float
b:float
c:float=field(init=False)
def__post_init__(self):
self.c=self.a+self.b
See the section below on init-only variables for ways to pass
parameters to__post_init__()
.Also see the warning about how
replace()
handlesinit=False
fields.
Class variables
One place wheredataclass
actually inspects the type of a field is
to determine if a field is a class variable as defined inPEP 526.It
does this by checking if the type of the field istyping.ClassVar
.
If a field is aClassVar
,it is excluded from consideration as a
field and is ignored by the Data Class mechanisms. For more
discussion, see[8].SuchClassVar
pseudo-fields are not
returned by the module-levelfields()
function.
Init-only variables
The other place wheredataclass
inspects a type annotation is to
determine if a field is an init-only variable. It does this by seeing
if the type of a field is of typedataclasses.InitVar
.If a field
is anInitVar
,it is considered a pseudo-field called an init-only
field. As it is not a true field, it is not returned by the
module-levelfields()
function. Init-only fields are added as
parameters to the generated__init__
method, and are passed to
the optional__post_init__
method. They are not otherwise used
by Data Classes.
For example, suppose a field will be initialized from a database, if a value is not provided when creating the class:
@dataclass
classC:
i:int
j:int=None
database:InitVar[DatabaseType]=None
def__post_init__(self,database):
ifself.jisNoneanddatabaseisnotNone:
self.j=database.lookup('j')
c=C(10,database=my_database)
In this case,fields()
will returnField
objects fori
and
j
,but not fordatabase
.
Frozen instances
It is not possible to create truly immutable Python objects. However,
by passingfrozen=True
to the@dataclass
decorator you can
emulate immutability. In that case, Data Classes will add
__setattr__
and__delattr__
methods to the class. These
methods will raise aFrozenInstanceError
when invoked.
There is a tiny performance penalty when usingfrozen=True
:
__init__
cannot use simple assignment to initialize fields, and
must useobject.__setattr__
.
Inheritance
When the Data Class is being created by the@dataclass
decorator,
it looks through all of the class’s base classes in reverse MRO (that
is, starting atobject
) and, for each Data Class that it finds,
adds the fields from that base class to an ordered mapping of fields.
After all of the base class fields are added, it adds its own fields
to the ordered mapping. All of the generated methods will use this
combined, calculated ordered mapping of fields. Because the fields
are in insertion order, derived classes override base classes. An
example:
@dataclass
classBase:
x:Any=15.0
y:int=0
@dataclass
classC(Base):
z:int=10
x:int=15
The final list of fields is, in order,x
,y
,z
.The final
type ofx
isint
,as specified in classC
.
The generated__init__
method forC
will look like:
def__init__(self,x:int=15,y:int=0,z:int=10):
Default factory functions
If a field specifies adefault_factory
,it is called with zero
arguments when a default value for the field is needed. For example,
to create a new instance of a list, use:
l:list=field(default_factory=list)
If a field is excluded from__init__
(usinginit=False
) and
the field also specifiesdefault_factory
,then the default factory
function will always be called from the generated__init__
function. This happens because there is no other way to give the
field an initial value.
Mutable default values
Python stores default member variable values in class attributes. Consider this example, not using Data Classes:
classC:
x=[]
defadd(self,element):
self.x+=element
o1=C()
o2=C()
o1.add(1)
o2.add(2)
asserto1.x==[1,2]
asserto1.xiso2.x
Note that the two instances of classC
share the same class
variablex
,as expected.
Using Data Classes,ifthis code was valid:
@dataclass
classD:
x:List=[]
defadd(self,element):
self.x+=element
it would generate code similar to:
classD:
x=[]
def__init__(self,x=x):
self.x=x
defadd(self,element):
self.x+=element
assertD().xisD().x
This has the same issue as the original example using classC
.
That is, two instances of classD
that do not specify a value for
x
when creating a class instance will share the same copy of
x
.Because Data Classes just use normal Python class creation
they also share this problem. There is no general way for Data
Classes to detect this condition. Instead, Data Classes will raise a
TypeError
if it detects a default parameter of typelist
,
dict
,orset
.This is a partial solution, but it does protect
against many common errors. SeeAutomatically support mutable
default valuesin the Rejected Ideas section for more details.
Using default factory functions is a way to create new instances of mutable types as default values for fields:
@dataclass
classD:
x:list=field(default_factory=list)
assertD().xisnotD().x
Module level helper functions
fields(class_or_instance)
:Returns a tuple ofField
objects that define the fields for this Data Class. Accepts either a Data Class, or an instance of a Data Class. RaisesValueError
if not passed a Data Class or instance of one. Does not return pseudo-fields which areClassVar
orInitVar
.asdict(instance,*,dict_factory=dict)
:Converts the Data Classinstance
to a dict (by using the factory functiondict_factory
). Each Data Class is converted to a dict of its fields, as name:value pairs. Data Classes, dicts, lists, and tuples are recursed into. For example:@dataclass classPoint: x:int y:int @dataclass classC: l:List[Point] p=Point(10,20) assertasdict(p)=={'x':10,'y':20} c=C([Point(0,0),Point(10,4)]) assertasdict(c)=={'l':[{'x':0,'y':0},{'x':10,'y':4}]}
Raises
TypeError
ifinstance
is not a Data Class instance.astuple(*,tuple_factory=tuple)
:Converts the Data Classinstance
to a tuple (by using the factory functiontuple_factory
). Each Data Class is converted to a tuple of its field values. Data Classes, dicts, lists, and tuples are recursed into.Continuing from the previous example:
assertastuple(p)==(10,20) assertastuple(c)==([(0,0),(10,4)],)
Raises
TypeError
ifinstance
is not a Data Class instance.make_dataclass(cls_name,fields,*,bases=(),namespace=None)
: Creates a new Data Class with namecls_name
,fields as defined infields
,base classes as given inbases
,and initialized with a namespace as given innamespace
.fields
is an iterable whose elements are eithername
,(name,type)
,or(name,type,Field)
.If justname
is supplied,typing.Any
is used fortype
.This function is not strictly required, because any Python mechanism for creating a new class with__annotations__
can then apply thedataclass
function to convert that class to a Data Class. This function is provided as a convenience. For example:C=make_dataclass('C', [('x',int), 'y', ('z',int,field(default=5))], namespace={'add_one':lambdaself:self.x+1})
Is equivalent to:
@dataclass classC: x:int y:'typing.Any' z:int=5 defadd_one(self): returnself.x+1
replace(instance,**changes)
:Creates a new object of the same type ofinstance
,replacing fields with values fromchanges
. Ifinstance
is not a Data Class, raisesTypeError
.If values inchanges
do not specify fields, raisesTypeError
.The newly returned object is created by calling the
__init__
method of the Data Class. This ensures that__post_init__
,if present, is also called.Init-only variables without default values, if any exist, must be specified on the call to
replace
so that they can be passed to__init__
and__post_init__
.It is an error for
changes
to contain any fields that are defined as havinginit=False
.AValueError
will be raised in this case.Be forewarned about how
init=False
fields work during a call toreplace()
.They are not copied from the source object, but rather are initialized in__post_init__()
,if they’re initialized at all. It is expected thatinit=False
fields will be rarely and judiciously used. If they are used, it might be wise to have alternate class constructors, or perhaps a customreplace()
(or similarly named) method which handles instance copying.is_dataclass(class_or_instance)
:Returns True if its parameter is a dataclass or an instance of one, otherwise returns False.If you need to know if a class is an instance of a dataclass (and not a dataclass itself), then add a further check for
not isinstance(obj,type)
:defis_dataclass_instance(obj): returnis_dataclass(obj)andnotisinstance(obj,type)
Discussion
python-ideas discussion
This discussion started on python-ideas[9]and was moved to a GitHub repo[10]for further discussion. As part of this discussion, we made the decision to usePEP 526syntax to drive the discovery of fields.
Support for automatically setting__slots__
?
At least for the initial release,__slots__
will not be supported.
__slots__
needs to be added at class creation time. The Data
Class decorator is called after the class is created, so in order to
add__slots__
the decorator would have to create a new class, set
__slots__
,and return it. Because this behavior is somewhat
surprising, the initial version of Data Classes will not support
automatically setting__slots__
.There are a number of
workarounds:
- Manually add
__slots__
in the class definition. - Write a function (which could be used as a decorator) that inspects
the class using
fields()
and creates a new class with__slots__
set.
For more discussion, see[11].
Why not just use namedtuple?
- Any namedtuple can be accidentally compared to any other with the
same number of fields. For example:
Point3D(2017,6,2)== Date(2017,6,2)
.With Data Classes, this would return False. - A namedtuple can be accidentally compared to a tuple. For example,
Point2D(1,10)==(1,10)
.With Data Classes, this would return False. - Instances are always iterable, which can make it difficult to add
fields. If a library defines:
Time=namedtuple('Time',['hour','minute']) defget_time(): returnTime(12,0)
Then if a user uses this code as:
hour,minute=get_time()
then it would not be possible to add a
second
field toTime
without breaking the user’s code. - No option for mutable instances.
- Cannot specify default values.
- Cannot control which fields are used for
__init__
,__repr__
, etc. - Cannot support combining fields by inheritance.
Why not just use typing.NamedTuple?
For classes with statically defined fields, it does support similar
syntax to Data Classes, using type annotations. This produces a
namedtuple, so it sharesnamedtuple
s benefits and some of its
downsides. Data Classes, unliketyping.NamedTuple
,support
combining fields via inheritance.
Why not just use attrs?
- attrs moves faster than could be accommodated if it were moved in to the standard library.
- attrs supports additional features not being proposed here: validators, converters, metadata, etc. Data Classes makes a tradeoff to achieve simplicity by not implementing these features.
For more discussion, see[12].
post-init parameters
In an earlier version of this PEP beforeInitVar
was added, the
post-init function__post_init__
never took any parameters.
The normal way of doing parameterized initialization (and not just with Data Classes) is to provide an alternate classmethod constructor. For example:
@dataclass
classC:
x:int
@classmethod
deffrom_file(cls,filename):
withopen(filename)asfl:
file_value=int(fl.read())
returnC(file_value)
c=C.from_file('file.txt')
Because the__post_init__
function is the last thing called in the
generated__init__
,having a classmethod constructor (which can
also execute code immediately after constructing the object) is
functionally equivalent to being able to pass parameters to a
__post_init__
function.
WithInitVar
s,__post_init__
functions can now take
parameters. They are passed first to__init__
which passes them
to__post_init__
where user code can use them as needed.
The only real difference between alternate classmethod constructors
andInitVar
pseudo-fields is in regards to required non-field
parameters during object creation. WithInitVar
s, using
__init__
and the module-levelreplace()
functionInitVar
s
must always be specified. Consider the case where acontext
object is needed to create an instance, but isn’t stored as a field.
With alternate classmethod constructors thecontext
parameter is
always optional, because you could still create the object by going
through__init__
(unless you suppress its creation). Which
approach is more appropriate will be application-specific, but both
approaches are supported.
Another reason for usingInitVar
fields is that the class author
can control the order of__init__
parameters. This is especially
important with regular fields andInitVar
fields that have default
values, as all fields with defaults must come after all fields without
defaults. A previous design had all init-only fields coming after
regular fields. This meant that if any field had a default value,
then all init-only fields would have to have defaults values, too.
asdict and astuple function names
The names of the module-level helper functionsasdict()
and
astuple()
are arguably notPEP 8compliant, and should be
as_dict()
andas_tuple()
,respectively. However, after
discussion[13]it was decided to keep consistency with
namedtuple._asdict()
andattr.asdict()
.
Rejected ideas
Copyinginit=False
fields after new object creation in replace()
Fields that areinit=False
are by definition not passed to
__init__
,but instead are initialized with a default value, or by
calling a default factory function in__init__
,or by code in
__post_init__
.
A previous version of this PEP specified thatinit=False
fields
would be copied from the source object to the newly created object
after__init__
returned, but that was deemed to be inconsistent
with using__init__
and__post_init__
to initialize the new
object. For example, consider this case:
@dataclass
classSquare:
length:float
area:float=field(init=False,default=0.0)
def__post_init__(self):
self.area=self.length*self.length
s1=Square(1.0)
s2=replace(s1,length=2.0)
Ifinit=False
fields were copied from the source to the
destination object after__post_init__
is run, then s2 would end
up beginSquare(length=2.0,area=1.0)
,instead of the correct
Square(length=2.0,area=4.0)
.
Automatically support mutable default values
One proposal was to automatically copy defaults, so that if a literal
list[]
was a default value, each instance would get a new list.
There were undesirable side effects of this decision, so the final
decision is to disallow the 3 known built-in mutable types: list,
dict, and set. For a complete discussion of this and other options,
see[14].
Examples
Custom __init__ method
Sometimes the generated__init__
method does not suffice. For
example, suppose you wanted to have an object to store*args
and
**kwargs
:
@dataclass(init=False)
classArgHolder:
args:List[Any]
kwargs:Mapping[Any,Any]
def__init__(self,*args,**kwargs):
self.args=args
self.kwargs=kwargs
a=ArgHolder(1,2,three=3)
A complicated example
This code exists in a closed source project:
classApplication:
def__init__(self,name,requirements,constraints=None,path='',executable_links=None,executables_dir=()):
self.name=name
self.requirements=requirements
self.constraints={}ifconstraintsisNoneelseconstraints
self.path=path
self.executable_links=[]ifexecutable_linksisNoneelseexecutable_links
self.executables_dir=executables_dir
self.additional_items=[]
def__repr__(self):
returnf'Application({self.name!r},{self.requirements!r},{self.constraints!r},{self.path!r},{self.executable_links!r},{self.executables_dir!r},{self.additional_items!r})'
This can be replaced by:
@dataclass
classApplication:
name:str
requirements:List[Requirement]
constraints:Dict[str,str]=field(default_factory=dict)
path:str=''
executable_links:List[str]=field(default_factory=list)
executable_dir:Tuple[str]=()
additional_items:List[str]=field(init=False,default_factory=list)
The Data Class version is more declarative, has less code, supports
typing
,and includes the other generated functions.
Acknowledgements
The following people provided invaluable input during the development of this PEP and code: Ivan Levkivskyi, Guido van Rossum, Hynek Schlawack, Raymond Hettinger, and Lisa Roach. I thank them for their time and expertise.
A special mention must be made about theattrs
project. It was a
true inspiration for this PEP, and I respect the design decisions they
made.
References
Copyright
This document has been placed in the public domain.
Source:https://github.com/python/peps/blob/main/peps/pep-0557.rst
Last modified:2023-09-09 17:39:29 GMT