The simple solution is to just implement the default arguments in __post_init__() only!

@dataclass
class Specs2:
    a: str
    b: str
    c: str

    def __post_init__(self):
        if self.b is None:
            self.b = 'Bravo'
        if self.c is None:
            self.c = 'Charlie'

(Code is not tested. If I got some detail wrong, it wouldn't be the first time)

Answer from Lars P on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ dataclasses.html
dataclasses โ€” Data Classes
3 weeks ago - By default, it is set to the module name of the caller. The decorator parameter is a callable that will be used to create the dataclass. It should take the class object as a first argument and the same keyword arguments as @dataclass.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 72654545 โ€บ python-add-dataclass-to-set
Python Add Dataclass to Set - Stack Overflow
Instead of using a set, I think it makes more sense to use a dict type in this case. This should be O(1) for lookup, and also this way we can avoid next to find an element by its Speaker hashed value. Example below. from pprint import pprint from typing import List from dataclasses import dataclass, field @dataclass class Speaker: id: int name: str statements: List[str] = field(default_factory=list) def __eq__(self, other): return self.id == other.id and self.name == other.name def __hash__(self): return hash((self.id, self.name)) test = [(1, 'john', 'foo'), (1, 'john', 'bar'), (2, 'jane', 'near'), (2, 'george', 'far')] speakers = {} for id, name, statement in test: key = Speaker(id, name) speaker = speakers.setdefault(key, key) speaker.statements.append(statement) print(speakers) print() pprint(list(speakers.values()))
Discussions

How to apply default value to Python dataclass field when None was passed? - Stack Overflow
3 How can I find out whether a field in a dataclass has the default value or whether it's explicitly set? 5 Calling a method when initializing a default value for a field in a dataclass ยท 6 Python dataclass: can you set a default default for fields? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Dataclasses - Sentinel to Stop creating "Field" instances - Ideas - Discussions on Python.org
I was wondering whether the following would be a good idea from dataclasses import dataclass, field, KW_ONLY, NO_FIELD @dataclass class Example: a: bool = True _: KW_ONLY kw_a: bool: = False b: str = field(init=False, default="hello") _: NO_FIELD c: str = "hello" After NO_FIELD the coder can ... More on discuss.python.org
๐ŸŒ discuss.python.org
1
April 16, 2024
Dataclasses: subclassing a dataclass without its fields inherited as init-fields
I was wondering if it would be possible to allow subclassing a dataclass without automatically including its fields in Subclass.__init__ (in some sense, hiding the inherited fields). When subclassing the dataclass AB below to create CD, the fields of AB become fields of CD, automatically included ... More on discuss.python.org
๐ŸŒ discuss.python.org
0
August 12, 2024
Dataclass single reference to created field
Hi, Iโ€™m not sure if this is a bug or intentional behavior, but I stubbed my toe on this in some production code and wanted to see if there was a better way to address it. I have a dataclass class that should have a property that is an instance of another class @dataclass class FirstClass: ... More on discuss.python.org
๐ŸŒ discuss.python.org
0
November 18, 2022
๐ŸŒ
Florimond Manca
florimond.dev โ€บ en โ€บ posts โ€บ 2018 โ€บ 10 โ€บ reconciling-dataclasses-and-properties-in-python
Reconciling Dataclasses And Properties In Python - Florimond Manca
October 10, 2018 - In short, dataclasses are a simple, elegant, Pythonic way of creating classes that hold data. ๐Ÿ ... I sometimes resort to the @property decorator to implement specific logic when getting/setting an attribute.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ data-classes-in-python-set-3-dataclass-fields
Data Classes in Python | Set 3 (dataclass fields) - GeeksforGeeks
July 11, 2025 - A way to set default value should be provided when init is set to False. repr : If true (the default), this field is included in the string returned by the generated __repr__() method. compare : If true (the default), this field is included in the generated equality and comparison methods (__eq__(), __gt__(), et al.). ... from dataclasses import dataclass, field @dataclass class GfgArticle: title: str = field(compare = False) author: str = field(repr = False) language: str = field(default ='Python3') upvotes: int = field(init = False, default = 0) # DataClass objects # Note the difference in their title value article1 = GfgArticle("DataClass", "vibhu4agarwal") article2 = GfgArticle("Python Packaging", "vibhu4agarwal") print(article1) print(article1.author) print(article1 == article2) Output:
๐ŸŒ
Medium
medium.com โ€บ alan โ€บ 5-things-you-should-know-about-dataclass-8c143b75596
๐Ÿ 5 things you should know about @dataclass | by Benoit Prioux | Alan Product and Technical Blog | Medium
July 5, 2022 - When declaring a dataclass, you inherit from a default set of behavior (see documentation for the full list). Personally, I was a bit surprised by the default mutability of Python dataclass: you can update the value of the property after initialization.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ customizing-dataclass-initialization
Customizing dataclass initialization - Python Morsels
October 18, 2024 - All classes inherit from the built-in object class, and the __setattr__ method on object does the actual attribute-setting behind the scenes. So we can make new instances of this dataclass without an exception being raised: ... And we still can't directly assign to any of the attributes on this new dataclass because it's frozen, which is exactly what we want: >>> r.width = 5 Traceback (most recent call last): File "<python-input-3>", line 1, in <module> r.width = 5 ^^^^^^^ File "<string>", line 17, in __setattr__ dataclasses.FrozenInstanceError: cannot assign to field 'width'
๐ŸŒ
Hamy
hamy.xyz โ€บ blog โ€บ 2023-10-python-dataclasses
Python Dataclass best practices (and why you should use them) - HAMY
The generic term for the pattern Python's dataclass fill is called a record. Records are typically lean data-only structures containing a fixed set of named, typed fields.
Find elsewhere
๐ŸŒ
InfoWorld
infoworld.com โ€บ home โ€บ software development โ€บ programming languages โ€บ python
How to use Python dataclasses | InfoWorld
October 22, 2025 - In this example, weโ€™ve created a __post_init__ method to set shelf_id to None if the bookโ€™s condition is initialized as "Discarded". Note how we use field to initialize shelf_id, and pass init as False to field. This means shelf_id wonโ€™t be initialized in __init__, but it is registered as a field with the dataclass overall, with type information. Another way to customize Python dataclass setup is to use the InitVar type.
๐ŸŒ
Python
peps.python.org โ€บ pep-0557
PEP 557 โ€“ Data Classes | peps.python.org
If either __getattr__ or __setattr__ is defined in the class, then ValueError is raised. See the discussion below. fields may optionally specify a default value, using normal Python syntax: @dataclass class C: a: int # 'a' has no default value b: int = 0 # assign a default value for 'b'
๐ŸŒ
Dataquest
dataquest.io โ€บ blog โ€บ how-to-use-python-data-classes
How to Use Python Data Classes (A Beginner's Guide) โ€“ Dataquest
May 12, 2025 - As we saw above, when using the dataclass decorator, the __init__, __repr__, and __eq__ methods are implemented for us. The creation of all these methods is set by the init, repr, and eq parameters of dataclass. These three parameters are True by default.
๐ŸŒ
Real Python
realpython.com โ€บ python-data-classes
Data Classes in Python (Guide) โ€“ Real Python
March 8, 2024 - from dataclasses import make_dataclass Position = make_dataclass('Position', ['name', 'lat', 'lon']) A data class is a regular Python class. The only thing that sets it apart is that it has basic data model methods like .__init__(), .__repr__(), and .__eq__() implemented for you.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-data-classes
Python Data Classes: A Comprehensive Tutorial | DataCamp
March 15, 2024 - Letโ€™s cover some of the fundamental concepts of Python data classes that make the so useful. Despite all their features, data classes are regular classes that take much less code to implement the same functionality. Here is the Exercise class again: from dataclasses import dataclass @dataclass class Exercise: name: str reps: int sets: int weight: float ex1 = Exercise("Bench press", 10, 3, 52.5) # Verifying Exercise is a regular class ex1.name 'Bench press'
๐ŸŒ
Ritviknag
dcw.ritviknag.com โ€บ en โ€บ latest โ€บ using_field_properties.html
Using Field Properties โ€” Dataclass Wizard 0.39.1 documentation
Using the Annotated type from the typing module (introduced in Python 3.9) it is possible to set a default value for the field property in the annotation itself. This is done by adding a field extra in the Annotated definition as shown below. from dataclasses import dataclass, field from typing import Annotated, Union from dataclass_wizard import property_wizard @dataclass class Vehicle(metaclass=property_wizard): wheels: Annotated[Union[int, str], field(default=4)] # Uncomment the field below if you want to make your IDE a bit happier.
๐ŸŒ
Medium
medium.com โ€บ @apps.merkurev โ€บ dataclasses-in-python-edca8c4b4242
Dataclasses in Python. Learn about the different ways toโ€ฆ | by AM | Medium
September 7, 2023 - The class syntax can be used with typing.NamedTuple starting from Python 3.6. This allows you to add your own methods or override existing ones, for example, define your own __str__ method. Book is still a subclass of tuple. import datetime from dataclasses import dataclass @dataclass(frozen=True) class Book: title: str author: str isbn: int pub_date: datetime.datetime rating: float = 4.98 # Book is not a subclass of tuple print(issubclass(Book, tuple)) # output: False book = Book( title='Learning Python', author='Mark Lutz', isbn=1449355730, pub_date=datetime.date(2013, 7, 30), ) print(book.title) # Learning Python print(book.rating) # 4.98, by default print(type(book)) # <class '__main__.Book'> print(book) # Book(title='Learning Python', author='Mark...)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ data-classes-in-python-an-introduction
Data Classes in Python | An Introduction - GeeksforGeeks
April 23, 2021 - What is the JustPy Module of Python?The ... Dataclasses is an inbuilt Python module which contains decorators and functions for automatically adding special methods like __init__() and __repr__() to user-defined classes....
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Dataclasses - Sentinel to Stop creating "Field" instances - Ideas - Discussions on Python.org
April 16, 2024 - I was wondering whether the following would be a good idea from dataclasses import dataclass, field, KW_ONLY, NO_FIELD @dataclass class Example: a: bool = True _: KW_ONLY kw_a: bool: = False b: str = field(init=False, default="hello") _: NO_FIELD c: str = "hello" After NO_FIELD the coder can define as many class attributes as needed without the attributes being considered for __init__ (and actions liks as_tuple) After having crafted many dataclasses I often find my...
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-23753.html
Creating a set with dataclass and dict
Hi everyone. I am trying to play with the new @dataclass and TypedDict from 3.7 and 3.8 But after searching and reading lots of documentation I cannot find a proper answer on this. I think the answer is simple but it's not clear to me. Here the snipp...
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Dataclasses: subclassing a dataclass without its fields inherited as init-fields - Python Help - Discussions on Python.org
August 12, 2024 - I was wondering if it would be possible to allow subclassing a dataclass without automatically including its fields in Subclass.__init__ (in some sense, hiding the inherited fields). When subclassing the dataclass AB below to create CD, the fields of AB become fields of CD, automatically included ...
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Dataclass single reference to created field - Python Help - Discussions on Python.org
November 18, 2022 - Hi, Iโ€™m not sure if this is a bug or intentional behavior, but I stubbed my toe on this in some production code and wanted to see if there was a better way to address it. I have a dataclass class that should have a property that is an instance of another class @dataclass class FirstClass: second_class: SecondClass = SecondClass() I would expect every instance of FirstClass to have itโ€™s own instance of SecondClass here, what actually ends up happening though is every instance of FirstClas...