0% found this document useful (0 votes)
30 views

Create Data Classes in Python

Uploaded by

Teto Schedule
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Create Data Classes in Python

Uploaded by

Teto Schedule
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Creating Data Classes in Python | Python in Plain English https://python.plainenglish.io/create-data-classes-in-python-6...

1 of 7 9/21/2021, 9:58 AM
Creating Data Classes in Python | Python in Plain English https://python.plainenglish.io/create-data-classes-in-python-6...

class Point():
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z

__eq__

__repr__

def __eq__(self, other):


if type(other) == LongPoint:
return (self.x == other.x and self.y == other.y and
self.z == other.z)
return False

def __repr__(self) -> str:


return "Point("+str(self.x)+", "+str(self.y) + ",
"+str(self.z)+")"

__ge__

__gt__ __le__ __lt__ __hash__

@dataclass

from dataclasses import dataclass

2 of 7 9/21/2021, 9:58 AM
Creating Data Classes in Python | Python in Plain English https://python.plainenglish.io/create-data-classes-in-python-6...

@dataclass
class Point():
x : int
y : int
z : int

__init__

__eq__ __repr__

>>> p = Point(1,2,3)
>>> print(p)
Point(x=1, y=2, z=3)

@dataclass
class Point():
# non-optional parameters
x : int
y : int
# optional parameters
z : int = 0

field

dataclasses

from dataclasses import dataclass, field

@dataclass

3 of 7 9/21/2021, 9:58 AM
Creating Data Classes in Python | Python in Plain English https://python.plainenglish.io/create-data-classes-in-python-6...

class User():
id : str
name : str = field(default = "unknown")
subscribed: list = field(default_factory=list)

"unknown"

list()

__init__ __repr__ __eq__

@dataclass
class User():
id : int
name : str
email : str

@dataclass(eq = False)
class User2():
id : int
name : str
email : str

def __eq__(self, o: object) -> bool:

4 of 7 9/21/2021, 9:58 AM
Creating Data Classes in Python | Python in Plain English https://python.plainenglish.io/create-data-classes-in-python-6...

if type(o) == User2:
return self.id == o.id and self.name == o.name and
self.email == o.email
return False

eq=False __eq__

id name

email field()

@dataclass
class User():
id : int
name : str
email : str = field(compare=False)

email

False

True __lt__ __le__ __gt__

__ge__ < <= > >=

@dataclass(order = True)
class User():
id : int
name : str
email : str

5 of 7 9/21/2021, 9:58 AM
Creating Data Classes in Python | Python in Plain English https://python.plainenglish.io/create-data-classes-in-python-6...

id

name email

__gt__

def __gt__(self, o):


if type(o) != User:
raise TypeError
return self.id > self.id or
(self.id == o.id and self.name > o.name) or
(self.id == o.id and self.name == o.name and
self.email > o.email)

@dataclass(order=True)
class User():
id : int
name : str
email : str = field(compare=False)# The email will not
be compared

False

True __hash__

6 of 7 9/21/2021, 9:58 AM
Creating Data Classes in Python | Python in Plain English https://python.plainenglish.io/create-data-classes-in-python-6...

eq=True frozen=True

dataclasses

7 of 7 9/21/2021, 9:58 AM

You might also like