I want to inherit to the back with python dataclass

I want a common field for each data class, but I want to give a default value, so if I inherit it normally, this will be attached before type error. It's hard

No good

from dataclasses import dataclass

@dataclass
class Base:
   base: int = 10

@dataclass
class Extend(Base):
    extend: int

type error

Also, even if you inherit a class that is not @ dataclass, it seems that the inherited field is not inherited (to \ _ \ _ init \ _ \ _).

To be honest, it's hard to read, but it's a solution

Write a decorator

from dataclasses import dataclass

def add(c):
    @dataclass
    class wrap(c):
        added: int = 10
    return wrap

@add
@dataclass
class DataClass:
    field: str

The downside is that the interpreter is <locals> .wrap instead of the name.

>> DataClass
<class '__main__.add.<locals>.wrap'>

Postscript

Apparently the name of the class is in __qualname__.

def add(c):
    @dataclass
    class wrap(c):
        added: int = 10
        __qualname__ = c.__qualname__
    return wrap
>> DataClass
<class '__main__.DataClass'>

Recommended Posts

I want to inherit to the back with python dataclass
I want to debug with Python
[Python] I want to use the -h option with argparse
I want to play with aws with python
I want to know the weather with LINE bot feat.Heroku + Python
I want to output the beginning of the next month with Python
I want to use MATLAB feval with python
I want to make a game with Python
I want to use Temporary Directory with Python2
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
I want to write to a file with Python
I want to display the progress in Python!
I want to handle optimization with python and cplex
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I want to work with a robot in python.
I want to write in Python! (3) Utilize the mock
I want to AWS Lambda with Python on Mac!
[ML Ops] I want to do multi-project with Python
I tried to solve the problem with Python Vol.1
I want to use the R dataset in python
I want to run a quantum computer with Python
I liked the tweet with python. ..
I want to do ○○ with Pandas
I tried to find the entropy of the image with python
I want to be able to analyze data with Python (Part 3)
I want to initialize if the value is empty (python)
I tried to simulate how the infection spreads with Python
I want to specify another version of Python with pyvenv
I wanted to solve the Panasonic Programming Contest 2020 with Python
I want to be able to analyze data with Python (Part 1)
I want to change the Japanese flag to the Palau flag with Numpy
I want to be able to analyze data with Python (Part 4)
What I did to welcome the Python2 EOL with confidence
I want to be able to analyze data with Python (Part 2)
I want to automatically attend online classes with Python + Selenium!
I want to know the features of Python and pip
I tried to divide the file into folders with Python
I want to pin Spyder to the taskbar
I want to detect objects with OpenCV
I want to output to the console coolly
I want to use a wildcard that I want to shell with Python remove
I want to monitor UNIQLO + J page updates [Scraping with python]
I want to handle the rhyme part1
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to blog with Jupyter Notebook
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
I want to handle the rhyme part3
I want to use jar from python
I want to build a Python environment
I want to pip install with PythonAnywhere
I want to do a full text search with elasticsearch + python
I wanted to solve the ABC164 A ~ D problem with Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I want to display the progress bar
For the time being, I want to convert files with ffmpeg !!
I want to check the position of my face with OpenCV!
I tried to improve the efficiency of daily work with Python
I wanted to solve ABC172 with Python
I want to handle the rhyme part2