Skip to content

Commit 2e54a19

Browse files
committed
Data Classes module completed
1 parent 7044571 commit 2e54a19

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Python Object Oriented Programming by Joe Marini course example
2+
# Using data classes to represent data objects
3+
4+
from dataclasses import dataclass
5+
#data classes are available for python 3.7 or later
6+
7+
@dataclass
8+
class Book:
9+
title : str
10+
author : str
11+
pages : int
12+
price : float
13+
14+
def book_info(self):
15+
return f"{self.title} by {self.author}"
16+
17+
# create some instances
18+
b1 = Book("War and Peace", "Leo Tolstoy", 1225, 39.95)
19+
b2 = Book("The Catcher in the Rye", "JD Salinger", 234, 29.95)
20+
b3 = Book("War and Peace", "Leo Tolstoy", 1225, 39.95)
21+
22+
# access fields
23+
print(b1.title)
24+
print(b2.author)
25+
26+
# print the book itself - dataclasses implement __repr__
27+
# dataclass automatically creat __repr__
28+
print(b1)
29+
30+
# comparing two dataclasses - they implement __eq__
31+
print(b1 == b3)
32+
print(b1 == b2)
33+
34+
# TODO: change some fields
35+
b3.author = "JK.Rowling"
36+
b3.title = "Harry Potter"
37+
print(b3.book_info())
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Python Object Oriented Programming by Joe Marini course example
2+
# implementing default values in data classes
3+
4+
from dataclasses import dataclass, field
5+
import random
6+
7+
def price_func():
8+
return float(random.randrange(20, 40))
9+
10+
11+
@dataclass
12+
class Book:
13+
# you can define default values when attributes are declared
14+
title: str = "No Title"
15+
author: str = "No Author"
16+
pages: int = 0
17+
price: float = field(default_factory= price_func)
18+
19+
20+
21+
b1 = Book("Harry Potter", "JK.Rowling", 14366)
22+
b2 = Book("Flip it", "M Heppel", 212)
23+
24+
print(b1)
25+
print(b2)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Python Object Oriented Programming by Joe Marini course example
2+
# Creating immutable data classes
3+
4+
from dataclasses import dataclass
5+
6+
7+
@dataclass(frozen=True) # "The "frozen" parameter makes the class immutable
8+
class ImmutableClass:
9+
value1: str = "Value 1"
10+
value2: int = 0
11+
12+
def somefunc(self, newvalue):
13+
self.value2 = newvalue
14+
15+
obj = ImmutableClass()
16+
print(obj.value1)
17+
print(obj)
18+
19+
# attempting to change the value of an immutable class throws an exception
20+
obj.value1 = "Some other value"
21+
print(obj.value1)
22+
23+
# even functions within the class can't change anything
24+
obj.somefunc(50)
25+
print(obj)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Python Object Oriented Programming by Joe Marini course example
2+
# Using the postinit function in data classes
3+
4+
from dataclasses import dataclass
5+
6+
7+
@dataclass
8+
class Book:
9+
title: str
10+
author: str
11+
pages: int
12+
price: float
13+
14+
# the __post_init__ function lets us customize additional properties
15+
# after the object has been initialized via built-in __init__
16+
def __post_init__(self):
17+
self.description = f"{self.title} by {self.author}, {self.pages} pages"
18+
19+
# create some Book objects
20+
b1 = Book("War and Peace", "Leo Tolstoy", 1225, 39.95)
21+
b2 = Book("The Catcher in the Rye", "JD Salinger", 234, 29.95)
22+
23+
# use the description attribute
24+
print(b1.description)
25+
print(b2.description)

0 commit comments

Comments
 (0)