question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[REQ] Allow overriding `__init__` of struct.dataclass

See original GitHub issue

Python 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:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jheekcommented, May 3, 2021

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,).

0reactions
marcvanzeecommented, May 4, 2021

@jheek Would it make sense to move this to a GH Discussion?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found