Class inheritance in Python 3.7 dataclasses

Cover Image for Class inheritance in Python 3.7 dataclasses
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

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! 🚀


More Stories

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

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 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

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# 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

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# 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

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# 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

Matheus Mello
Matheus Mello