Dataclases en Python

Clases regulares

class Perro:
nombre: str
raza: str
edad: int

def __init__(self, nombre, raza, edad):
self.nombre = nombre
self.raza = raza
self.edad = edad
mascota1 = Perro('RUDY', 'Terrier Australiano', 2)
mascota2 = Perro('TEDDY', 'bichón frisé', 2)
mascota3 = Perro('TEDDY', 'bichón frisé', 2)
mascota2 == mascota3

False
print(mascota2)


<__main__.Perro object at 0x7f9c104e7640>
print(mascota2.nombre,mascota2.raza,mascota2.edad)

TEDDY bichón frisé 2

Data clases

pip install dataclasses
from dataclasses import dataclass
@dataclass
class Perro:
nombre: str
raza: str
edad: int
mascota1 = Perro('RUDY', 'Terrier Australiano', 2)
mascota2 = Perro('TEDDY', 'bichón frisé', 2)
mascota3 = Perro('TEDDY', 'bichón frisé', 2)
print(mascota2 == mascota3)

True
print(mascota2)

Perro(nombre='TEDDY', raza='bichón frisé', edad=2)
print(mascota2.nombre,mascota2.raza,mascota2.edad)

TEDDY bichón frisé 2
@dataclass
class Perro:
nombre: str
raza: str
edad: int

def __repr__(self):
return f"{self.nombre} {self.raza} ({self.edad})"


mascota1 = Perro('RUDY', 'Terrier Australiano', 2)
mascota2 = Perro('TEDDY', 'bichón frisé', 2)
mascota3 = Perro('TEDDY', 'bichón frisé', 2)


print(mascota2)

TEDDY bichón frisé (2)
print(mascota1  > mascota2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_603591/468771088.py in <module>
----> 1 mascota1 > mascota2

TypeError: '>' not supported between instances of 'Perro' and 'Perro'
from dataclasses import dataclass, field

@dataclass
class Perro:
sort_index: int = field(init=False)
nombre: str
raza: str
edad: int

def __repr__(self):
return f"{self.sort_index} {self.nombre} {self.raza} ({self.edad})"

def __post_init__(self):
self.sort_index = self.edad
mascota1 = Perro('RUDY', 'Terrier Australiano', 3)
mascota2 = Perro('TEDDY', 'bichón frisé', 2)
print(mascota2)

#2 TEDDY bichón frisé (2)


print(mascota2.sort_index,mascota2.nombre,mascota2.raza,mascota2.edad)

#2 TEDDY bichón frisé 2
print(mascota1 > mascota2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_603591/4142115589.py in <module>
----> 1 mascota1 > mascota2

TypeError: '>' not supported between instances of 'Perro' and 'Perro'

Clases de solo lectura

@dataclass(order=True, frozen=True)
class Perro:
sort_index: int = field(init=False)
nombre: str
raza: str
edad: int

def __post_init__(self):
object.__setattr__(self, 'sort_index', self.edad)

mascota1 = Perro('RUDY', 'Terrier Australiano', 3)
mascota2 = Perro('TEDDY', 'bichón frisé', 2)
print(mascota1  > mascota2)

True
print(mascota2)


Perro(sort_index=2, nombre='TEDDY', raza='bichón frisé', edad=2)
mascota2.edad = 4--------------------------------------------------------------------------- FrozenInstanceError Traceback (most recent call last) /tmp/ipykernel_603591/927787478.py in <module> ----> 1 mascota2.edad = 4 <string> in __setattr__(self, name, value) FrozenInstanceError: cannot assign to field 'edad'

--

--

Senior Python Developer, Software Architect, Big Data, Data Science, Machine Learning and Deep Learning, Debian SysAdmin, DevOps, Blogger and Freelancer.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ernesto Crespo

Ernesto Crespo

Senior Python Developer, Software Architect, Big Data, Data Science, Machine Learning and Deep Learning, Debian SysAdmin, DevOps, Blogger and Freelancer.