Why don’t we also add dataclasses.POS_ONLY as well? Anything before it is
positional argument.
@dataclasses.dataclass
class LotsOfFields:
x: Any
y: Any
z: Any
_:dataclasses.POS_ONLY
a: Any
__: dataclasses.KW_ONLY
b: Any = 0
c: Any = 'foo'
d: Any
e: Any = 0.0
f: Any
g: Any = ()
h: Any = 'bar'
i: Any = 3+4j
j: Any = 10
k: Any
The generated __init__ would look like:
def __init__(self, x, y, z, /, a, *, b=0, c='foo', d, e=0.0, f, g=(), h='bar',
i=3+4j, j=10, k):
Similar to dataclasses.KW_ONLY, you can use dataclasses.POS_ONLY only once. It
should always come before dataclasses.KW_ONLY. Anything else, it should raise
an error. If dataclasses.POS_ONLY came right after the class declaration (no
instance attribute preceding it), an error is generated similar to what we have
right now. Inheritance is build up (from super class to sub class) of three
groups of arguments: pos_only arguments together, then mixed arguments, and
then kw_only arguments. repr should work the same way. We would like to
reconstruct the object from repr. We don’t want too much change from current
behavior.
Lets create a class that inherits from the above class..
@dataclasses.dataclass
class LessFields(LotsOfFields):
x2: Any
_: dataclasses.POS_ONLY
a2: Any
__:dataclasses.KW_ONLY
b2: Any
The generated __init__ would look like:
def __init__(self, x, y, z, x2, /, a, a2, *, b=0, c='foo', d, e=0.0, f, g=(),
h='bar', i=3+4j, j=10, k, b2):
Another class..
@dataclasses.dataclass
Class EvenLessFields(LessFields):
a3: Any
_: dataclasses.KW_ONLY
b3: Any = 9
The generated __init__ would look like:
def __init__(self, x, y, z, x2, /, a, a2, a3, *, b=0, c='foo', d, e=0.0, f,
g=(), h='bar', i=3+4j, j=10, k, b2, b3=9):
All arguments are ordered from the super super class until the subclass all in
their respective path or group of arguments.
Abdulla
> On 16 Mar 2021, at 1:18 AM, Eric V. Smith <[email protected]> wrote:
>
> @dataclasses.dataclass
> class LotsOfFields:
> a: Any
> _: dataclasses.KW_ONLY
> b: Any = 0
> c: Any = 'foo'
> d: Any
> e: Any = 0.0
> f: Any
> g: Any = ()
> h: Any = 'bar'
> i: Any = 3+4j
> j: Any = 10
> k: Any
>
> Which I think is a lot clearer.
>
> The generated __init__ would look like:
>
> def __init__(self, a, *, b=0, c='foo', d, e=0.0, f, g=(), h='bar', i=3+4j,
> j=10, k):
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/5SA2XVRAGCPHTVTFO3WCD36BDARY2MTA/
Code of Conduct: http://python.org/psf/codeofconduct/