Created
August 4, 2021 09:39
-
-
Save Louis95/ec5773c5517db879c890441e51dc92a5 to your computer and use it in GitHub Desktop.
Code snippet demonstrating the use of data classes in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass, field | |
@dataclass(order=True,frozen=True) | |
class Person: | |
sort_index: int = field(init=False, repr=False) | |
name: str | |
job: str | |
age: int | |
strength: int = 100 | |
def __post_init__(self): | |
object.__setattr__(self, "sort_index", self.strength) | |
def __str__(self): | |
return f'{self.name}, ({self.age}), {self.job}' | |
person1 = Person("Louis", "developer", 35,34) | |
person2 = Person("Gerald", "designer", 25) | |
person3 = Person("Gerald", "designer", 25) | |
print(id(person2)) | |
print(id(person3)) | |
print(person1) | |
print(person2 == person3) | |
print(person1 > person2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment