-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass variable and instance variable.py
53 lines (39 loc) · 1.48 KB
/
class variable and instance variable.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
53
class Water:
boiling_temperature = 100 # class variable
freezing_temperature = 0 # class variable
def __init__(self,temperature):
self.temperature = temperature # instance variable
def state(self):
if self.temperature <= self.freezing_temperature:
return 'solid'
if self.freezing_temperature < self.temperature < self.boiling_temperature:
return 'liquid'
if self.temperature > self.boiling_temperature:
return 'gas'
# if a user of your class uses a different temperature system (e.g., Fahrenheit),
water = Water(temperature=20)
water.boiling_temperature = 212
water.freezing_temperature = 32
print(water.state())
******************************************************************************************************************************************************
class Matter:
boiling_temperature = None
freezing_temperature = None
def __init__(self, temperature):
self.temperature = temperature
def state(self):
if self.temperature <= self.freezing_temperature:
return 'solid'
elif self.freezing_temperature < self.temperature < self.boiling_temperature:
return 'liquid'
else:
return 'gas'
class Water(Matter):
boiling_temperature = 100
freezing_temperature = 0
class Mercury(Matter):
boiling_temperature = 356.7
freezing_temperature = -38.83
# example
Water(20).state()
# NOT Water.state(20)