[New-bugs-announce] [issue38444] dataclass: always generate default __init__ on __default_init__
Shmuel H.
report at bugs.python.org
Fri Oct 11 06:39:38 EDT 2019
New submission from Shmuel H. <shmuelhazan0 at gmail.com>:
Currently, `dataclasses.dataclass` will generate `__init__` only where the user has not defined one.
However, sometimes, with frozen classes or dataclasses with a lot of members, redefinition of this function is not trivial,
especially if the only purpose is to change the default behaviour for only one member:
```python
from dataclasses import dataclass
@dataclass(frozen=True)
class Dataclass:
#...big list of members
member20: int
def __init__(self, member20: str, **kwargs):
# self.member20 = int(member20)
object.__setattr__(self, "member20", int(member20))
# Now we have to trivially initialize
# 20 other members like that :[
```
My idea is to generate the default `__init__` into `__default_init__` even, if the user has defined their own version.
That will allow them to use it like that:
```python
from dataclasses import dataclass
@dataclass(frozen=True)
class Dataclass:
#...big list of members
member20: int
def __init__(self, member20: str, **kwargs):
# Oh, that's better :)
self.__default_init__(member20=int(member20), **kwargs)
```
Implementing that is pretty trivial (I can do that if this change will be approved).
Please let me know what you think about that.
----------
components: Library (Lib)
messages: 354437
nosy: Shmuel H.
priority: normal
severity: normal
status: open
title: dataclass: always generate default __init__ on __default_init__
type: enhancement
versions: Python 3.7, Python 3.8, Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38444>
_______________________________________
More information about the New-bugs-announce
mailing list