9
9
# First Example
10
10
11
11
class Warrior (object ):
12
-
12
+
13
13
# delta position - class variable
14
14
delta_position = 10
15
15
#number of warriors
16
16
num_of_warriors = 0
17
-
18
-
17
+
19
18
def __init__ (self ,posx ,posy ):
20
19
self .posx = posx
21
20
self .posy = posy
22
-
21
+
23
22
Warrior .num_of_warriors += 1
24
-
25
-
23
+
26
24
def move (self , posx ,posy ):
27
25
self .posx = posx + self .delta_position
28
26
self .posy = posy + self .delta_position
29
-
27
+
30
28
def position (self ):
31
29
return '{} , {}' .format (self .posx ,self .posy )
32
-
30
+
33
31
# Second Example
34
32
35
33
class Monster (object ):
36
-
37
-
34
+
38
35
# delta position - class variable
39
36
delta_position = 10
40
37
#number of monters
41
38
num_of_monsters = 0
42
-
43
- def __init__ (self ,posx ,posy ):
39
+
40
+ def __init__ (self , posx , posy ):
44
41
self .posx = posx
45
42
self .posy = posy
46
-
47
- Monster .num_of_monsters += 1
48
-
43
+
44
+ Monster .num_of_monsters += 1
45
+
49
46
def move (self , posx ,posy ):
50
- self .posx = posx + self .delta_position
51
- self .posy = posy + self .delta_position
52
-
47
+ self .posx = posx + self .delta_position
48
+ self .posy = posy + self .delta_position
49
+
53
50
def position (self ):
54
51
return '{} , {}' .format (self .posx ,self .posy )
55
-
52
+
56
53
# used to change the value of class variable
57
54
# same as Monster.delta_position = new_value
58
55
@classmethod
@@ -61,53 +58,51 @@ def set_move(cls,inc):
61
58
62
59
#using classmethod as alternatives consctructor
63
60
@classmethod
64
- def from_str (cls ,pos_str ):
61
+ def from_str (cls , pos_str ):
65
62
posx , posy = pos_str .split ("-" )
66
- return cls (posx ,posy )
67
-
68
- #static methods
63
+ return cls (posx , posy )
64
+
65
+ #static methods
69
66
#behaves like a regular function
70
67
@staticmethod
71
68
def is_gameday (day ):
72
69
if day .weekday == 5 or day .weekday == 6 :
73
70
return False
74
71
return True
75
-
76
-
77
-
72
+
78
73
if __name__ == "__main__" :
79
-
74
+
80
75
# First Example
81
-
76
+
82
77
# declare an instance of Warrior Class
83
- Savage = Warrior (20 ,10 )
84
- Savage .move (10 ,25 )
78
+ Savage = Warrior (20 , 10 )
79
+ Savage .move (10 , 25 )
85
80
print (Savage .position ())
86
-
81
+
87
82
# print namespace of instance
88
- print (Savage .__dict__ )
89
-
83
+ print (Savage .__dict__ )
84
+
90
85
# print namespace of class Warrior
91
- print (Warrior .__dict__ )
86
+ print (Warrior .__dict__ )
92
87
# print number of objects
93
- print (Warrior .num_of_warriors )
94
-
88
+ print (Warrior .num_of_warriors )
89
+
95
90
# Second Example
96
-
97
- monster = Monster (30 ,20 )
91
+
92
+ monster = Monster (30 , 20 )
98
93
print (monster .position ())
99
94
# change value of class variable
100
95
Monster .set_move (5 )
101
- monster .move (10 ,10 )
96
+ monster .move (10 , 10 )
102
97
print (monster .position ())
103
-
98
+
104
99
# using the "alternative constructor"
105
100
second_monster = Monster .from_str ("10-30" )
106
101
print (second_monster .__dict__ )
107
-
102
+
108
103
# to test staticmethod
109
104
now = datetime .datetime .now ()
110
- print (Monster .is_gameday (now ))
105
+ print (Monster .is_gameday (now ))
111
106
112
107
113
108
0 commit comments