Movatterモバイル変換


[0]ホーム

URL:


Following system colour schemeSelected dark colour schemeSelected light colour scheme

Python Enhancement Proposals

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

Table of Contents

Notice for Reviewers

This PEP and the initial implementation were drafted in a separaterepo:https://github.com/ericvsmith/dataclasses. Before commenting ina public forum please at least read thediscussion listed at theend of this PEP.

Abstract

This PEP describes an addition to the standard library called DataClasses. Although they use a very different mechanism, Data Classescan be thought of as “mutable namedtuples with defaults”. BecauseData Classes use normal class definition syntax, you are free to useinheritance, metaclasses, docstrings, user-defined methods, classfactories, and other Python class features.

A class decorator is provided which inspects a class definition forvariables with type annotations as defined inPEP 526, “Syntax forVariable Annotations”. In this document, such variables are calledfields. Using these fields, the decorator adds generated methoddefinitions to the class to support instance initialization, a repr,comparison methods, and optionally other methods as described in theSpecification section. Such a class is called a Data Class, butthere’s really nothing special about the class: the decorator addsgenerated methods to the class and returns the same class it wasgiven.

As an example:

@dataclassclassInventoryItem:'''Class for keeping track of an item in inventory.'''name:strunit_price:floatquantity_on_hand:int=0deftotal_cost(self)->float:returnself.unit_price*self.quantity_on_hand

The@dataclass decorator will add the equivalent of these methodsto the InventoryItem class:

def__init__(self,name:str,unit_price:float,quantity_on_hand:int=0)->None:self.name=nameself.unit_price=unit_priceself.quantity_on_hand=quantity_on_handdef__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)returnNotImplementeddef__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)returnNotImplementeddef__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)returnNotImplementeddef__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)returnNotImplementeddef__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)returnNotImplementeddef__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 existprimarily 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 inspiredby collections.namedtuple.
  • Many example online recipes[3], packages[4], and questions[5].David Beazley used a form of data classes as the motivating examplein a PyCon 2013 metaclass talk[6].

So, why is this PEP needed?

With the addition ofPEP 526, Python has a concise way to specify thetype of class members. This PEP leverages that syntax to provide asimple, unobtrusive way to describe Data Classes. With two exceptions,the specified attribute type annotation is completely ignored by DataClasses.

No base classes or metaclasses are used by Data Classes. Users ofthese classes are free to use inheritance and metaclasses without anyinterference from Data Classes. The decorated classes are truly“normal” Python classes. The Data Class decorator should notinterfere with any usage of the class.

One main design goal of Data Classes is to support static typecheckers. The use ofPEP 526 syntax is one example of this, but so isthe design of thefields() function and the@dataclassdecorator. Due to their very dynamic nature, some of the librariesmentioned above are difficult to use with static type checkers.

Data Classes are not, and are not intended to be, a replacementmechanism for all of the above libraries. But being in the standardlibrary will allow many of the simpler use cases to instead leverageData Classes. Many of the libraries listed have different featuresets, 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 isrequired, or value validation or conversion is required.

Specification

All of the functions described in this PEP will live in a module nameddataclasses.

A functiondataclass which is typically used as a class decoratoris provided to post-process classes and add generated methods,described below.

Thedataclass decorator examines the class to findfields. Afield 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 machineryexamines 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 thegenerated methods is the order in which they appear in the class.

Thedataclass decorator will add various “dunder” methods to theclass, described below. If any of the added methods already exist on theclass, aTypeError will be raised. The decorator returns the sameclass that is called on: no new class is created.

Thedataclass decorator is typically used with no parameters andno parentheses. However, it also supports the following logicalsignature:

defdataclass(*,init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False)

Ifdataclass is used just as a simple decorator with noparameters, it acts as if it has the default values documented in thissignature. That is, these three uses of@dataclass are equivalent:

@dataclassclassC:...@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 begenerated.
  • repr: If true (the default), a__repr__ method will begenerated. The generated repr string will have the class name andthe name and repr of each field, in the order they are defined inthe class. Fields that are marked as being excluded from the reprare not included. For example:InventoryItem(name='widget',unit_price=3.0,quantity_on_hand=10).

    If the class already defines__repr__, this parameter isignored.

  • eq: If true (the default), an__eq__ method will begenerated. This method compares the class as if it were a tuple of itsfields, in order. Both instances in the comparison must be of theidentical 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 comparethe class as if it were a tuple of its fields, in order. Bothinstances 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__ methodis generated according to howeq andfrozen are set.

    Ifeq andfrozen are both true, Data Classes will generate a__hash__ method for you. Ifeq is true andfrozen isfalse,__hash__ will be set toNone, marking it unhashable(which it is). Ifeq is false,__hash__ will be leftuntouched meaning the__hash__ method of the superclass will beused (if the superclass isobject, this means it will fall backto id-based hashing).

    Although not recommended, you can force Data Classes to create a__hash__ method withunsafe_hash=True. This might be thecase if your class is logically immutable but can nonetheless bemutated. This is a specialized use case and should be consideredcarefully.

    If a class already has an explicitly defined__hash__ thebehavior when adding__hash__ is modified. An explicitlydefined__hash__ is defined when:

    • __eq__ is defined in the class and__hash__ is definedwith 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__ isdefined.

    Ifunsafe_hash is true and an explicitly defined__hash__is present, thenValueError is raised.

    Ifunsafe_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 willgenerate an exception. This emulates read-only frozen instances.If either__getattr__ or__setattr__ is defined in theclass, thenValueError is raised. See the discussion below.

fields may optionally specify a default value, using normalPython syntax:

@dataclassclassC:a:int# 'a' has no default valueb: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 valuefollows a field with a default value. This is true either when thisoccurs 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 additionalper-field information. To satisfy this need for additionalinformation, you can replace the default field value with a call tothe 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 thedefault anddefault_factory parameters are provided. Thissentinel is used becauseNone is a valid value fordefault.

The parameters tofield() are:

  • default: If provided, this will be the default value for thisfield. This is needed because thefield call itself replacesthe normal position of the default value.
  • default_factory: If provided, it must be a zero-argumentcallable that will be called when a default value is needed for thisfield. Among other purposes, this can be used to specify fieldswith mutable default values, as discussed below. It is an error tospecify bothdefault anddefault_factory.
  • init: If true (the default), this field is included as aparameter to the generated__init__ method.
  • repr: If true (the default), this field is included in thestring returned by the generated__repr__ method.
  • compare: If True (the default), this field is included in thegenerated equality and comparison methods (__eq__,__gt__,et al.).
  • hash: This can be a bool orNone. If True, this field isincluded in the generated__hash__ method. IfNone (thedefault), use the value ofcompare: this would normally be theexpected behavior. A field should be considered in the hash ifit’s used for comparisons. Setting this value to anything otherthanNone is discouraged.

    One possible reason to sethash=False butcompare=True wouldbe if a field is expensive to compute a hash value for, that fieldis needed for equality testing, and there are other fields thatcontribute to the type’s hash value. Even if a field is excludedfrom the hash, it will still be used for comparisons.

  • metadata: This can be a mapping or None. None is treated as anempty dict. This value is wrapped intypes.MappingProxyType tomake it read-only, and exposed on the Field object. It is not usedat all by Data Classes, and is provided as a third-party extensionmechanism. Multiple third-parties can each have their own key, touse 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 thespecifieddefault value. If nodefault is provided, then theclass attribute will be deleted. The intent is that after thedataclass decorator runs, the class attributes will all containthe default values for the fields, just as if the default value itselfwere specified. For example, after:

@dataclassclassC:x:inty:int=field(repr=False)z:int=field(repr=False,default=10)t:int=20

The class attributeC.z will be10, the class attributeC.t will be20, and the class attributesC.x andC.ywill not be set.

Field objects

Field objects describe each defined field. These objects arecreated internally, and are returned by thefields() module-levelmethod (see below). Users should never instantiate aFieldobject 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 valuesas they do in thefield() declaration.

Other attributes may exist, but they are private and must not beinspected 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 calledasself.__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 thatdepend on one or more other fields. For example:

@dataclassclassC:a:floatb:floatc: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 passparameters to__post_init__(). Also see the warning about howreplace() handlesinit=False fields.

Class variables

One place wheredataclass actually inspects the type of a field isto determine if a field is a class variable as defined inPEP 526. Itdoes this by checking if the type of the field istyping.ClassVar.If a field is aClassVar, it is excluded from consideration as afield and is ignored by the Data Class mechanisms. For morediscussion, see[8]. SuchClassVar pseudo-fields are notreturned by the module-levelfields() function.

Init-only variables

The other place wheredataclass inspects a type annotation is todetermine if a field is an init-only variable. It does this by seeingif the type of a field is of typedataclasses.InitVar. If a fieldis anInitVar, it is considered a pseudo-field called an init-onlyfield. As it is not a true field, it is not returned by themodule-levelfields() function. Init-only fields are added asparameters to the generated__init__ method, and are passed tothe optional__post_init__ method. They are not otherwise usedby Data Classes.

For example, suppose a field will be initialized from a database, if avalue is not provided when creating the class:

@dataclassclassC:i:intj:int=Nonedatabase:InitVar[DatabaseType]=Nonedef__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 andj, but not fordatabase.

Frozen instances

It is not possible to create truly immutable Python objects. However,by passingfrozen=True to the@dataclass decorator you canemulate immutability. In that case, Data Classes will add__setattr__ and__delattr__ methods to the class. Thesemethods will raise aFrozenInstanceError when invoked.

There is a tiny performance penalty when usingfrozen=True:__init__ cannot use simple assignment to initialize fields, andmust 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 (thatis, 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 fieldsto the ordered mapping. All of the generated methods will use thiscombined, calculated ordered mapping of fields. Because the fieldsare in insertion order, derived classes override base classes. Anexample:

@dataclassclassBase:x:Any=15.0y:int=0@dataclassclassC(Base):z:int=10x:int=15

The final list of fields is, in order,x,y,z. The finaltype 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 zeroarguments 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) andthe field also specifiesdefault_factory, then the default factoryfunction will always be called from the generated__init__function. This happens because there is no other way to give thefield 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+=elemento1=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 classvariablex, as expected.

Using Data Classes,if this code was valid:

@dataclassclassD:x:List=[]defadd(self,element):self.x+=element

it would generate code similar to:

classD:x=[]def__init__(self,x=x):self.x=xdefadd(self,element):self.x+=elementassertD().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 forx when creating a class instance will share the same copy ofx. Because Data Classes just use normal Python class creationthey also share this problem. There is no general way for DataClasses to detect this condition. Instead, Data Classes will raise aTypeError if it detects a default parameter of typelist,dict, orset. This is a partial solution, but it does protectagainst many common errors. SeeAutomatically support mutabledefault values in the Rejected Ideas section for more details.

Using default factory functions is a way to create new instances ofmutable types as default values for fields:

@dataclassclassD:x:list=field(default_factory=list)assertD().xisnotD().x

Module level helper functions

  • fields(class_or_instance): Returns a tuple ofField objectsthat define the fields for this Data Class. Accepts either a DataClass, or an instance of a Data Class. RaisesValueError if notpassed a Data Class or instance of one. Does not returnpseudo-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 itsfields, as name:value pairs. Data Classes, dicts, lists, and tuplesare recursed into. For example:
    @dataclassclassPoint:x:inty:int@dataclassclassC: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}]}

    RaisesTypeError 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 itsfield values. Data Classes, dicts, lists, and tuples are recursedinto.

    Continuing from the previous example:

    assertastuple(p)==(10,20)assertastuple(c)==([(0,0),(10,4)],)

    RaisesTypeError 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 definedinfields, base classes as given inbases, and initializedwith a namespace as given innamespace.fields is aniterable whose elements are eithername,(name,type), or(name,type,Field). If justname is supplied,typing.Any is used fortype. This function is not strictlyrequired, because any Python mechanism for creating a new class with__annotations__ can then apply thedataclass function toconvert that class to a Data Class. This function is provided as aconvenience. For example:
    C=make_dataclass('C',[('x',int),'y',('z',int,field(default=5))],namespace={'add_one':lambdaself:self.x+1})

    Is equivalent to:

    @dataclassclassC:x:inty:'typing.Any'z:int=5defadd_one(self):returnself.x+1
  • replace(instance,**changes): Creates a new object of the sametype ofinstance, replacing fields with values fromchanges.Ifinstance is not a Data Class, raisesTypeError. Ifvalues 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 bespecified on the call toreplace so that they can be passed to__init__ and__post_init__.

    It is an error forchanges to contain any fields that aredefined as havinginit=False. AValueError will be raisedin this case.

    Be forewarned about howinit=False fields work during a call toreplace(). They are not copied from the source object, butrather are initialized in__post_init__(), if they’reinitialized at all. It is expected thatinit=False fields willbe rarely and judiciously used. If they are used, it might be wiseto have alternate class constructors, or perhaps a customreplace() (or similarly named) method which handles instancecopying.

  • is_dataclass(class_or_instance): Returns True if its parameteris a dataclass or an instance of one, otherwise returns False.

    If you need to know if a class is an instance of a dataclass (andnot a dataclass itself), then add a further check fornotisinstance(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 GitHubrepo[10] for further discussion. As part of this discussion, we madethe decision to usePEP 526 syntax 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 DataClass decorator is called after the class is created, so in order toadd__slots__ the decorator would have to create a new class, set__slots__, and return it. Because this behavior is somewhatsurprising, the initial version of Data Classes will not supportautomatically setting__slots__. There are a number ofworkarounds:

  • Manually add__slots__ in the class definition.
  • Write a function (which could be used as a decorator) that inspectsthe class usingfields() 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 thesame 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 returnFalse.
  • Instances are always iterable, which can make it difficult to addfields. 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 asecond field toTimewithout 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 similarsyntax to Data Classes, using type annotations. This produces anamedtuple, so it sharesnamedtuples benefits and some of itsdownsides. Data Classes, unliketyping.NamedTuple, supportcombining fields via inheritance.

Why not just use attrs?

  • attrs moves faster than could be accommodated if it were moved in tothe standard library.
  • attrs supports additional features not being proposed here:validators, converters, metadata, etc. Data Classes makes atradeoff to achieve simplicity by not implementing thesefeatures.

For more discussion, see[12].

post-init parameters

In an earlier version of this PEP beforeInitVar was added, thepost-init function__post_init__ never took any parameters.

The normal way of doing parameterized initialization (and not justwith Data Classes) is to provide an alternate classmethod constructor.For example:

@dataclassclassC:x:int@classmethoddeffrom_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 thegenerated__init__, having a classmethod constructor (which canalso execute code immediately after constructing the object) isfunctionally equivalent to being able to pass parameters to a__post_init__ function.

WithInitVars,__post_init__ functions can now takeparameters. They are passed first to__init__ which passes themto__post_init__ where user code can use them as needed.

The only real difference between alternate classmethod constructorsandInitVar pseudo-fields is in regards to required non-fieldparameters during object creation. WithInitVars, using__init__ and the module-levelreplace() functionInitVarsmust always be specified. Consider the case where acontextobject is needed to create an instance, but isn’t stored as a field.With alternate classmethod constructors thecontext parameter isalways optional, because you could still create the object by goingthrough__init__ (unless you suppress its creation). Whichapproach is more appropriate will be application-specific, but bothapproaches are supported.

Another reason for usingInitVar fields is that the class authorcan control the order of__init__ parameters. This is especiallyimportant with regular fields andInitVar fields that have defaultvalues, as all fields with defaults must come after all fields withoutdefaults. A previous design had all init-only fields coming afterregular 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() andastuple() are arguably notPEP 8 compliant, and should beas_dict() andas_tuple(), respectively. However, afterdiscussion[13] it was decided to keep consistency withnamedtuple._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 bycalling a default factory function in__init__, or by code in__post_init__.

A previous version of this PEP specified thatinit=False fieldswould be copied from the source object to the newly created objectafter__init__ returned, but that was deemed to be inconsistentwith using__init__ and__post_init__ to initialize the newobject. For example, consider this case:

@dataclassclassSquare:length:floatarea:float=field(init=False,default=0.0)def__post_init__(self):self.area=self.length*self.lengths1=Square(1.0)s2=replace(s1,length=2.0)

Ifinit=False fields were copied from the source to thedestination object after__post_init__ is run, then s2 would endup beginSquare(length=2.0,area=1.0), instead of the correctSquare(length=2.0,area=4.0).

Automatically support mutable default values

One proposal was to automatically copy defaults, so that if a literallist[] was a default value, each instance would get a new list.There were undesirable side effects of this decision, so the finaldecision 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. Forexample, 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=argsself.kwargs=kwargsa=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=nameself.requirements=requirementsself.constraints={}ifconstraintsisNoneelseconstraintsself.path=pathself.executable_links=[]ifexecutable_linksisNoneelseexecutable_linksself.executables_dir=executables_dirself.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:

@dataclassclassApplication:name:strrequirements: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, supportstyping, and includes the other generated functions.

Acknowledgements

The following people provided invaluable input during the developmentof this PEP and code: Ivan Levkivskyi, Guido van Rossum, HynekSchlawack, Raymond Hettinger, and Lisa Roach. I thank them for theirtime and expertise.

A special mention must be made about theattrs project. It was atrue inspiration for this PEP, and I respect the design decisions theymade.

References

[1]
attrs project on github(https://github.com/python-attrs/attrs)
[2]
George Sakkis’ recordType recipe(http://code.activestate.com/recipes/576555-records/)
[3]
DictDotLookup recipe(http://code.activestate.com/recipes/576586-dot-style-nested-lookups-over-dictionary-based-dat/)
[4]
attrdict package(https://pypi.python.org/pypi/attrdict)
[5]
StackOverflow question about data container classes(https://stackoverflow.com/questions/3357581/using-python-class-as-a-data-container)
[6]
David Beazley metaclass talk featuring data classes(https://www.youtube.com/watch?v=sPiWg5jSoZI)
[7]
Python documentation for __hash__(https://docs.python.org/3/reference/datamodel.html#object.__hash__)
[8]
ClassVar discussion in PEP 526
[9]
Start of python-ideas discussion(https://mail.python.org/pipermail/python-ideas/2017-May/045618.html)
[10]
GitHub repo where discussions and initial development took place(https://github.com/ericvsmith/dataclasses)
[11]
Support __slots__?(https://github.com/ericvsmith/dataclasses/issues/28)
[12]
why not just attrs?(https://github.com/ericvsmith/dataclasses/issues/19)
[13]
PEP 8 names for asdict and astuple(https://github.com/ericvsmith/dataclasses/issues/110)
[14]
Copying mutable defaults(https://github.com/ericvsmith/dataclasses/issues/3)

Copyright

This document has been placed in the public domain.


Source:https://github.com/python/peps/blob/main/peps/pep-0557.rst

Last modified:2025-02-01 08:55:40 GMT


[8]ページ先頭

©2009-2025 Movatter.jp