GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing python-course.eu — 5. Dataclasses In Python | OOP | python-course.eu 5. Dataclasses In PythonBy Bernd Klein. Last modified: 19 Feb 2025. On this page ➤ Journey through Dataclasses: Advanced Techniques with Python DataclassesWe delve into the powerful world of Python dataclasses in this chapter of our Python tutorial. Dataclasses are an essential feature introduced in Python 3.7 to simplify the creation and management of classes primarily used to store data. The chapter begins by pointing out the the difference of traditional class structures for representing data and introducing dataclasses as an attractive alternative. We will explore the syntax and functionalities of dataclasses and we will demonstrate how code readability will be enhanced. Readers will gain a comprehensive understanding of the principles underlying dataclasses and will be able to applicate them in their Python projects. Live Python training See our Python training courses First ExamplesIn our first example, we remain true to our beloved robot Marvin. So, we start with a "traditional" Python class class Robot_traditional: def __init__(self, model, serial_number, manufacturer): self.model = model self.serial_number = serial_number self.manufacturer = manufacturer The boilerplate code within the If we have a look at the same example in from dataclasses import dataclass @dataclass class Robot: model: str serial_number: str manufacturer: str Here, the dataclass decorator automates the generation of special methods like This example demonstrates how using dataclasses for a class primarily used to store data, such as a robot representation, offers a more streamlined and readable alternative to traditional class structures. Initializing robots of both classes is the same: x = Robot_traditional("NanoGuardian XR-2000", "234-76", "Cyber Robotics Co.") y = Robot("MachinaMaster MM-42", "986-42", "Quantum Automations Inc.") Yet, there are other differences in these classes. The class decorator Let's have a look at print(repr(x)) print(repr(y)) # uses __repr__ OUTPUT:<__main__.Robot_traditional object at 0x7f21c0acb410> Robot(model='MachinaMaster MM-42', serial_number='986-42', manufacturer='Quantum Automations Inc.') We can see that class Robot_traditional: def __init__(self, model, serial_number, manufacturer): self.model = model self.serial_number = serial_number self.manufacturer = manufacturer def __repr__(self): return f"Robot_traditional(model='{self.model}', serial_number='{self.serial_number}', manufacturer='{self.manufacturer}')" x = Robot_traditional("NanoGuardian XR-2000", "234-76", "Cyber Robotics Co.") print(repr(x)) OUTPUT:Robot_traditional(model='NanoGuardian XR-2000', serial_number='234-76', manufacturer='Cyber Robotics Co.') Immutable ClassesIn our chapter "Creating Immutable Classes in Python" of our tutorial, we discuss the rationale behind the need for immutability and explore different methods of creating them. It's very simple with dataclasses. All you have to do is to call the decorator with from dataclasses import dataclass @dataclass(frozen=True) class ImmutableRobot: name: str brandname: str We can use this class and test two robots for equality. They are equal, if all the attributes are the same. Remember that x1 = ImmutableRobot("Marvin", "NanoGuardian XR-2000") x2 = ImmutableRobot("Marvin", "NanoGuardian XR-2000") print(x1 == x2) OUTPUT:True Let's look at the hash values: print(x1.__hash__(), x2.__hash__()) OUTPUT:-7581403571593916930 -7581403571593916930 First, we can see that Now, let's implement a similar class without utilizing dataclass. In our previously defined class class ImmutableRobot_traditional: def __init__(self, name: str, brandname: str): self._name = name self._brandname = brandname @property def name(self) -> str: return self._name @property def brandname(self) -> str: return self._brandname def __eq__(self, other): if not isinstance(other, ImmutableRobot_traditional): return False return self.name == other.name and self.brandname == other.brandname def __hash__(self): return hash((self.name, self.brandname)) x1 = ImmutableRobot_traditional("Marvin", "NanoGuardian XR-2000") x2 = ImmutableRobot_traditional("Marvin", "NanoGuardian XR-2000") print(x1 == x2) OUTPUT:True print(x1.__hash__(), x2.__hash__()) OUTPUT:-7581403571593916930 -7581403571593916930 Immutable Classes for mappingsHaving an immutable class with a from dataclasses import dataclass @dataclass(frozen=True) class ImmutableRobot: name: str brandname: str robot1 = ImmutableRobot("Marvin", "NanoGuardian XR-2000") robot2 = ImmutableRobot("R2D2", "QuantumTech Sentinel-7") robot3 = ImmutableRobot("Marva", "MachinaMaster MM-42") # we create a set of Robots: robots = {robot1, robot2, robot3} print("The robots in the set robots:") for robo in robots: print(robo) # now a dictionary with robots as keys: activity = {robot1: 'activated', robot2: 'activated', robot3: 'deactivated'} print("\nAll the activated robots:") for robo, mode in activity.items(): if mode == 'activated': print(f"{robo} is activated") OUTPUT:The robots in the set robots: ImmutableRobot(name='Marva', brandname='MachinaMaster MM-42') ImmutableRobot(name='R2D2', brandname='QuantumTech Sentinel-7') ImmutableRobot(name='Marvin', brandname='NanoGuardian XR-2000') All the activated robots: ImmutableRobot(name='Marvin', brandname='NanoGuardian XR-2000') is activated ImmutableRobot(name='R2D2', brandname='QuantumTech Sentinel-7') is activated SummaryUsing a dataclass provides several benefits over traditional class definitions in Python:
Overall, Live Python training See our Python training courses ExercisesExercise 1: Book InformationCreate a
Write a program that does the following:
You can use this exercise to practice defining SolutionsSolution 1from dataclasses import dataclass @dataclass class Book: title: str author: str isbn: str publication_year: int genre: str # Create instances of several books book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", "9780743273565", 1925, "Fiction") book2 = Book("To Kill a Mockingbird", "Harper Lee", "9780061120084", 1960, "Fiction") book3 = Book("1984", "George Orwell", "9780451524935", 1949, "Science Fiction") # Print out the details of each book print("Book 1:") print("Title:", book1.title) print("Author:", book1.author) print("ISBN:", book1.isbn) print("Publication Year:", book1.publication_year) print("Genre:", book1.genre) print("\nBook 2:") print("Title:", book2.title) print("Author:", book2.author) print("ISBN:", book2.isbn) print("Publication Year:", book2.publication_year) print("Genre:", book2.genre) print("\nBook 3:") print("Title:", book3.title) print("Author:", book3.author) print("ISBN:", book3.isbn) print("Publication Year:", book3.publication_year) print("Genre:", book3.genre) OUTPUT:Book 1: Title: The Great Gatsby Author: F. Scott Fitzgerald ISBN: 9780743273565 Publication Year: 1925 Genre: Fiction Book 2: Title: To Kill a Mockingbird Author: Harper Lee ISBN: 9780061120084 Publication Year: 1960 Genre: Fiction Book 3: Title: 1984 Author: George Orwell ISBN: 9780451524935 Publication Year: 1949 Genre: Science Fiction |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |