-
Notifications
You must be signed in to change notification settings - Fork 512
/
Copy pathinstance_start.py
39 lines (30 loc) · 1.08 KB
/
instance_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
# Python Object Oriented Programming by Joe Marini course example
# Using instance methods and attributes
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
self.author = author
self.pages = pages
self.price = price
self.__secret = "This is a secret value."
# TODO: create instance 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
# TODO: create some book instances
b1 = Book("War and Peace","Leo Tolstoy",1225,39.95)
b2 = Book("The Catcher in the Rye","JD Salinger",234,29.95)
# TODO: print the price of book1
print(b1.getprice())
# TODO: try setting the discount
print(b2.getprice())
b2.setdiscount(0.25)
print(b2.getprice())
# TODO: properties with double underscores are hidden by the interpreter
print(b2._Book__secret)