Skip to content

Commit 0625340

Browse files
committed
OOP module
1 parent f918960 commit 0625340

File tree

4 files changed

+73
-38
lines changed

4 files changed

+73
-38
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Python Object Oriented Programming by Joe Marini course example
2+
# Using class-level and static methods
3+
4+
5+
class Book:
6+
# Properties defined at the class level are shared by all instances
7+
BOOK_TYPES = ("HARDCOVER", "PAPERBACK", "EBOOK")
8+
9+
# double-underscore properties are hidden from other classes
10+
__booklist = None
11+
12+
# create a class method
13+
@classmethod
14+
def getbooktypes(cls):
15+
return cls.BOOK_TYPES
16+
17+
# create a static method
18+
@staticmethod
19+
def getbooklist():
20+
if Book.__booklist == None:
21+
Book.__booklist = []
22+
return Book.__booklist
23+
24+
# instance methods receive a specific object instance as an argument
25+
# and operate on data specific to that object instance
26+
def setTitle(self, newtitle):
27+
self.title = newtitle
28+
29+
def __init__(self, title, booktype):
30+
self.title = title
31+
if booktype not in self.BOOK_TYPES:
32+
raise ValueError(f"{booktype} is not a valid booktype.")
33+
else:
34+
self.booktype = booktype
35+
36+
# access the class attribute
37+
print(Book.getbooktypes())
38+
39+
# Create some book instances
40+
b1 = Book("Title 1", "HARDCOVER")
41+
b2 = Book("Title 2", "PAPERBACK")
42+
43+
# Use the static method to access a singleton object
44+
thebooks = Book.getbooklist()
45+
thebooks.append(b1)
46+
thebooks.append(b2)
47+
print(thebooks)

12.Python Object Oriented Programming - JM/01.OOP/class_start.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

12.Python Object Oriented Programming - JM/01.OOP/instance.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,34 @@
55
class Book:
66
# the "init" function is called when the instance is
77
# created and ready to be initialized
8-
def __init__(self, title):
8+
def __init__(self, title, author, pages, price):
99
self.title = title
10-
# add properties
10+
self.author = author
11+
self.pages = pages
12+
self.price = price
13+
self.__secret = "This is secret attribute."
1114

1215
# create instance methods
16+
def getprice(self):
17+
if hasattr(self, "_discount"):
18+
return self.price - (self.price * self._discount)
19+
return self.price
20+
21+
def setdiscount(self, amount):
22+
self._discount = amount
1323

1424

1525
# create some book instances
16-
b1 = Book("War and Peace")
17-
b2 = Book("The Catcher in the Rye")
26+
b1 = Book("War and Peace", "Leo Tolstoy", 1225, 39.95)
27+
b2 = Book("The Catcher in the Rye", "JD Salinger", 234, 29.99)
1828

1929
# print the price of book1
20-
30+
print(b1.getprice())
2131

2232
# try setting the discount
23-
33+
b1.setdiscount(0.20)
34+
print("after discount : ", b1.getprice())
2435

2536
# properties with double underscores are hidden by the interpreter
37+
# print(b1.__secret)
38+
print(b1._Book__secret)

12.Python Object Oriented Programming - JM/01.OOP/typecheck_start.py renamed to 12.Python Object Oriented Programming - JM/01.OOP/typecheck.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ def __init__(self, name):
1919
n2 = Newspaper("The New York Times")
2020

2121
# use type() to inspect the object type
22-
22+
print(type(b1))
23+
print(type(n1))
2324

2425
# compare two types together
25-
26+
print(type(b1) == type(b2))
27+
print(type(b2) == type(n2))
2628

2729
# use isinstance to compare a specific instance to a known type
30+
print(isinstance(b1, Book))
31+
print(isinstance(b1, Newspaper))
32+
print(isinstance(b2, object))

0 commit comments

Comments
 (0)