-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path065.oops.py
More file actions
55 lines (44 loc) · 1.32 KB
/
065.oops.py
File metadata and controls
55 lines (44 loc) · 1.32 KB
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
53
54
55
class Kettle(object):
power_source = "electricity"
def __init__(self, make, price):
self.make = make
self.price = price
self.on = False
def switch_on(self):
self.on = True
kenwood = Kettle("Kenwood", 8.99)
print(kenwood.make)
print(kenwood.price)
kenwood.price = 12.75
print(kenwood.price)
print()
hamilton = Kettle("Hamilton", 14.55)
print("Models: {} = {}, {} = {}".format(kenwood.make, kenwood.price, hamilton.make, hamilton.price))
print("Models: {0.make} = {0.price}, {1.make} = {1.price}".format(kenwood, hamilton))
"""
Class: template for creating objects.All objects created using the same class will have the same Characteristics.
Object: an instance of an class
Instantiate: create an instance of an class
Method: a function defined in a class
Attribute: a variable bound to an instance of a class
"""
print(hamilton.on)
hamilton.switch_on()
print(hamilton.on)
Kettle.switch_on(kenwood)
print(kenwood.on)
kenwood.switch_on()
print("*"*80)
kenwood.power = 1.5
print(kenwood.power)
# print(hamilton.power)
print("Switch to atomic power")
Kettle.power_source = "atomic"
print(Kettle.power_source)
print("Switch kenwood to gas")
kenwood.power_source = "gas"
print(kenwood.power_source)
print(hamilton.power_source)
print(Kettle.__dict__)
print(kenwood.__dict__)
print(hamilton.__dict__)