[REQ] Allow overriding `__init__` of struct.dataclass
See original GitHub issuePython data classes allow the ovverriding of frozen data classes __init__
methods, that essentially just set all the relevant fields correctly.
I would like to ask that you expose that kwargument in your data class wrapper.
This is useful if a frozen data class is usually constructed starting from object_A but does not store object_A itself, but rather something computed starting from it.
We can get around this by using a constructor method, but for a series of reasons the easiest things for us to do in NetKet would be to just override the __init__
.
I don’t see reasons not to allow this, as it is allowed by the standard dataclass specification.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
[REQ] Allow overriding __init__ of struct.dataclass #1299
Python data classes allow the ovverriding of frozen data classes __init__ methods, that essentially just set all the relevant fields ...
Read more >Calling generated `__init__` in custom ... - Stack Overflow
Currently I have something like this: @dataclass(frozen=True) ...
Read more >dataclasses — Data Classes — Python 3.11.1 documentation
The newly returned object is created by calling the __init__() method of the dataclass. This ensures that __post_init__() , if present, is also...
Read more >Data classes | Kotlin
If the functions of the supertype cannot be overridden due to incompatible signatures or due to their being final, an error is reported....
Read more >Inheritance in Python Dataclass - Studytonight
As mentioned in point 2 of the above inference, the __init__() method is overridden from superclass to subclass. Suppose __init__ method is not ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can always define a vanilla dataclass with a custom flatten/unflatten. But in this case it’s your own responsibility to reconstruct without a trivial
constructor(*fields)
call.Disclaimer I’m a big Rust fanboy: But I think “smart” constructors are not a good pattern. IMO a constructor should always be simple and what you want instead is a classmethod that creates your dataclass from some other value. A simple argument for this is that once the constructor starts being smart you could imagine wanting multiple constructors. But to maintain sanity these constructors should probably have a self-explanatory name. So I think what you should do is something like:
FMJacobian.from_model(model, params, data)
The one exception to this could be simple type transformations that operate like a fixpoint. So for example transforming int -> (int,) such that when I pass in the (int,) again it stays (int,).
@jheek Would it make sense to move this to a GH Discussion?