-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
45 lines (32 loc) · 1.27 KB
/
classes.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
# Creat a Class
class Book:
# the "init" function is called when the instance is
# created and ready to be initialized
def __init__(self , title , author, pages , price):
self.title = title
# add properties
self.author = author
self.pages = pages
self.price = price
self.__secret = "This is a secret attribute"
# creat instances methods
def getPrice(self):
if hasattr(self, "_discount"):
return self.price - (self.price * self._discount)
else:
return self.price
def setDiscount(self, amount):
self._discount = amount
# Creat instances of the Class i.e Book instances
b1 = Book("War and Peace","Leo Tolstoy" , 1225 , 39.95)
b2 = Book("The Catcher in the Rye", "JD Salinger" , 234 , 29.95)
# Print the price of b1
print(b1.getPrice())
# try setting the discount
print(f"Price of b2 Before discount: {b2.getPrice()}")
b2.setDiscount(0.25)
print(f"Price of b2 After 25% discount: {b2.getPrice()}")
# Properties with double IUnderscore are hidden by the interpreter
print(b2.__secret) # cannot be accussed and gives an attributeError
# comment the above line to see the next lines result
print(b2._Book__secret) # this will give the output for hidden attributes