-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses_specialmethodspy.py
More file actions
43 lines (29 loc) · 999 Bytes
/
classes_specialmethodspy.py
File metadata and controls
43 lines (29 loc) · 999 Bytes
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
# -*- coding: utf-8 -*-
"""
Simple study related do classes magic methods - "dunders"
"""
# General Class
class Warrior(object):
# delta position - class variable
delta_position = 10
#number of warriors
num_of_warriors = 0
def __init__(self,posx,posy):
self.posx = posx
self.posy = posy
Warrior.num_of_warriors+=1
def move(self, posx,posy):
self.posx = posx+ self.delta_position
self.posy = posy +self.delta_position
def position(self):
return '{} , {}' .format(self.posx,self.posy)
# should be able ro recreate the object
def __repr__(self):
return "Warrior('{}','{}')".format(self.posx,self.posy)
#
def __str__(self):
return "'{}'-'{}'".format(self.posx,self.posy)
if __name__ =="__main__":
warrior = Warrior(10,20)
print(warrior.__repr__())
print(warrior.__str__())