Class inheritance in Python 3.7 dataclasses

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Class inheritance in Python 3.7 dataclasses

Class Inheritance in Python 3.7 Dataclasses: Handling Argument Order

If you're currently exploring the new dataclass constructions introduced in Python 3.7, you may come across situations where you need to handle class inheritance. This blog post will address a common issue related to argument order in class inheritance with dataclasses, provide an easy solution, and empower you to engage with the Python community.

The Problem: Argument Order and Type Error

Let's take a look at the provided code snippet:

from dataclasses import dataclass

@dataclass
class Parent:
    name: str
    age: int
    ugly: bool = False

    def print_name(self):
        print(self.name)

    def print_age(self):
        print(self.age)

    def print_id(self):
        print(f"The Name is {self.name} and {self.name} is {self.age} years old")

@dataclass
class Child(Parent):
    school: str
    ugly: bool = True


jack = Parent('jack snr', 32, ugly=True)
jack_son = Child('jack jnr', 12, school='Harvard', ugly=True)

jack.print_id()
jack_son.print_id()

When running this code, you encounter a TypeError with the following message:

TypeError: non-default argument 'school' follows default argument

The Solution: Reordering Arguments

The problem arises from the order of arguments in the child class, where the school argument is placed after the ugly argument, which has a default value. To fix this, we need to reorder the arguments.

from dataclasses import dataclass

@dataclass
class Parent:
    name: str
    age: int
    ugly: bool = False

    def print_name(self):
        print(self.name)

    def print_age(self):
        print(self.age)

    def print_id(self):
        print(f"The Name is {self.name} and {self.name} is {self.age} years old")

@dataclass
class Child(Parent):
    ugly: bool = True
    school: str  # Reordered argument


jack = Parent('jack snr', 32, ugly=True)
jack_son = Child('jack jnr', 12, ugly=True, school='Harvard')  # Reordered argument

jack.print_id()
jack_son.print_id()

By placing the ugly argument before the school argument in the child class, we prevent the TypeError from occurring.

Engage and Learn

Understanding class inheritance and argument order in dataclasses is crucial for writing clean and maintainable code. If you found this post helpful, let us know by leaving a comment or sharing it with fellow Python enthusiasts.

Have you encountered any other issues or challenges related to dataclasses in Python 3.7? Share your experiences and questions in the comments section below. Let's create a vibrant and supportive community that helps each other grow as Python developers!

Keep exploring, keep coding, and stay passionate about Python! 🚀

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my