-
Notifications
You must be signed in to change notification settings - Fork 512
/
Copy pathclass_start.py
51 lines (40 loc) · 1.45 KB
/
class_start.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Python Object Oriented Programming by Joe Marini course example
# Using class-level and static methods
class Book:
# TODO: Properties defined at the class level are shared by all instances
BOOK_TYPES = ("HARDCOVER","PAPERBACK","EBOOK")
# TODO: double-underscore properties are hidden from other classes
__booklist = None
# TODO: create a class method
@classmethod
def get_book_types(cls):
return cls.BOOK_TYPES
# TODO: create a static method
def getbooklist():
if Book.__booklist == None:
Book.__booklist = []
return Book.__booklist
# instance methods receive a specific object instance as an argument
# and operate on data specific to that object instance
def set_title(self, newtitle):
self.title = newtitle
def __init__(self, title, booktype):
self.title = title
if (not booktype in Book.BOOK_TYPES):
raise ValueError(f"{booktype} is not a valid book type!")
else:
self.booktype = booktype
# def __repr__(self):
# return f"Book(title={self.title})"
# TODO: access the class attribute
print("Book Types: ", Book.get_book_types())
# TODO: Create some book instances
b1 = (Book("Title1", "HARDCOVER"))
b2 = (Book("Title2", "PAPERBACK"))
# print(b1.title)
# print(b1.booktype)
# TODO: Use the static method to access a singleton object
thebooks = Book.getbooklist()
thebooks.append(b1)
thebooks.append(b2)
print(thebooks)