-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_static.py
52 lines (38 loc) · 1.34 KB
/
class_static.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
52
# Creat a Class
class Book:
# properties defined at classlevel are shared by All instances
BOOK_TYPES = ("HARDCOVER" ,"PAPERBACK","EBOOK")
# double_Underscore properties are hidden from other instances
__booklist = None
# creat a class method
@classmethod
def getbooktypes(cls):
return cls.BOOK_TYPES
# creat a static method
@staticmethod
def getbooklist():
if Book.__booklist == None:
Book.__booklist = []
return Book.__booklist
#instance methods recived a specific Object instance as an argument
#and operate on data specific to that object instance
def setTitle(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
# access the class attribute
print( "BOOK types : ", Book.getbooktypes())
# creat come Book instances
b1=Book("Title 1" , "HARDCOVER")
# b2=Book("Title 2" , "COMIC") # this will raise an ValueError: COMIC is not a valid book Type
#lest corret this
b2=Book("Title 2" , "PAPERBACK")
# USe the Static method to access a singlton object
thebooks = Book.getbooklist()
thebooks.append(b1)
thebooks.append(b2)
print(thebooks)