Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)
Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)

🔧 Programmierung 🕛 vor 2 Jahren 2 Min Lesezeit
0

Python Trick: Using dataclasses with field(default_factory=...)

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Python's dataclasses module simplifies the creation of classes used for storing data.



While most people know about basic usage, there’s a less-known feature field(default_factory=...) that can be incredibly useful for handling default values in mutable types.









How It Works



When defining a dataclass, you might want to use a mutable default value, such as a list or a dictionary.



Using a mutable default directly can lead to unexpected behavior due to the way default arguments are shared across instances.



The default_factory function provides a clean way to handle mutable defaults.



Here’s a simple example:




CODE
from dataclasses import dataclass, field
from typing import List


@dataclass
class Student:
name: str
grades: List[int] = field(default_factory=list) # Use default_factory for mutable default


# Create new Student instances
student1 = Student(name="Alice")
student2 = Student(name="Bob", grades=[90, 85])

# Modify student1's grades
student1.grades.append(95)

print(student1) # Output: Student(name='Alice', grades=[95])
print(student2) # Output: Student(name='Bob', grades=[90, 85])


# Output:
# Student(name='Alice', grades=[95])
# Student(name='Bob', grades=[90, 85])






In this example, grades is initialized with an empty list for each new Student instance.

Using field(default_factory=list) ensures that each instance gets its own separate list, avoiding the pitfalls of shared mutable defaults.









Why It’s Cool



The default_factory feature is invaluable for avoiding common issues with mutable default arguments.



It helps ensure that each instance of a dataclass has its own default value, making your code more predictable and avoiding subtle bugs related to shared state.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
Use custom web fonts in Google Sheets charts
2 Quellen
Introducing the new 1Password App for Google Chat
1 Quelle
Context-aware access controls are available for Gemini Enterprise in the Admin console
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Python Trick: Using dataclasses with field(default_factory=...)

Thematisch verwandte Begriffe: Python, Trick, Using, dataclasses · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...