Skip to content

Commit 1fe7547

Browse files
author
krsm
committed
cleaning spaces
1 parent 07c1a28 commit 1fe7547

File tree

1 file changed

+37
-42
lines changed

1 file changed

+37
-42
lines changed

classes_staticandclassmethods.py

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,47 @@
99
# First Example
1010

1111
class Warrior(object):
12-
12+
1313
# delta position - class variable
1414
delta_position = 10
1515
#number of warriors
1616
num_of_warriors = 0
17-
18-
17+
1918
def __init__(self,posx,posy):
2019
self.posx = posx
2120
self.posy = posy
22-
21+
2322
Warrior.num_of_warriors+=1
24-
25-
23+
2624
def move(self, posx,posy):
2725
self.posx = posx+ self.delta_position
2826
self.posy = posy +self.delta_position
29-
27+
3028
def position(self):
3129
return '{} , {}' .format(self.posx,self.posy)
32-
30+
3331
# Second Example
3432

3533
class Monster(object):
36-
37-
34+
3835
# delta position - class variable
3936
delta_position = 10
4037
#number of monters
4138
num_of_monsters = 0
42-
43-
def __init__(self,posx,posy):
39+
40+
def __init__(self, posx, posy):
4441
self.posx = posx
4542
self.posy = posy
46-
47-
Monster.num_of_monsters+=1
48-
43+
44+
Monster.num_of_monsters+= 1
45+
4946
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+
5350
def position(self):
5451
return '{} , {}' .format(self.posx,self.posy)
55-
52+
5653
# used to change the value of class variable
5754
# same as Monster.delta_position = new_value
5855
@classmethod
@@ -61,53 +58,51 @@ def set_move(cls,inc):
6158

6259
#using classmethod as alternatives consctructor
6360
@classmethod
64-
def from_str(cls,pos_str):
61+
def from_str(cls, pos_str):
6562
posx, posy = pos_str.split("-")
66-
return cls(posx,posy)
67-
68-
#static methods
63+
return cls(posx, posy)
64+
65+
#static methods
6966
#behaves like a regular function
7067
@staticmethod
7168
def is_gameday(day):
7269
if day.weekday == 5 or day.weekday == 6:
7370
return False
7471
return True
75-
76-
77-
72+
7873
if __name__ == "__main__":
79-
74+
8075
# First Example
81-
76+
8277
# 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)
8580
print(Savage.position())
86-
81+
8782
# print namespace of instance
88-
print (Savage.__dict__)
89-
83+
print(Savage.__dict__)
84+
9085
# print namespace of class Warrior
91-
print (Warrior.__dict__)
86+
print(Warrior.__dict__)
9287
# print number of objects
93-
print (Warrior.num_of_warriors)
94-
88+
print(Warrior.num_of_warriors)
89+
9590
# Second Example
96-
97-
monster = Monster(30,20)
91+
92+
monster = Monster(30, 20)
9893
print(monster.position())
9994
# change value of class variable
10095
Monster.set_move(5)
101-
monster.move(10,10)
96+
monster.move(10, 10)
10297
print(monster.position())
103-
98+
10499
# using the "alternative constructor"
105100
second_monster = Monster.from_str("10-30")
106101
print(second_monster.__dict__)
107-
102+
108103
# to test staticmethod
109104
now = datetime.datetime.now()
110-
print (Monster.is_gameday(now))
105+
print(Monster.is_gameday(now))
111106

112107

113108

0 commit comments

Comments
 (0)