-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_template.py
More file actions
52 lines (39 loc) · 1.35 KB
/
class_template.py
File metadata and controls
52 lines (39 loc) · 1.35 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
""" Class template
Ipea's Python 2022
"""
import random
# class name typically Capital letter
class Template:
# Usually has an __init__ method called at the moment of instance creation
def __init__(self, name, arg1, arg2):
# Armazena os parâmetros de início/da criação dentro daquela instância
# É comum ter uma ID de identificação única, ou nome
self.id = name
self.arg1 = arg1
self.arg2 = arg2
# Pode conter containers, data structures, planilhas do pandas
self.members = dict()
self.ranking = list()
# Ou ainda, um valor randômico
self.luck = random.randrange(1, 60)
def method1(self, quantia):
# Modifica um valor armazenado
self.arg1 += quantia
def method2(self, outro_agente):
# Pode comparar-se com outro agente e acessar métodos do outro agente
if self.arg1 > outro_agente.arg1:
return True
else:
return False
def method3(self):
# Um método pode acessar um outro método.
# Nesse caso, adicionando um valor aleatório ao arg1!
self.method1(self.luck)
if __name__ == '__main__':
# Sempre TESTE
a = Template(1, 23, 50)
a.method3()
b = Template(0, 50, 23)
a.method1(25)
print(b.method2(a))
print(f'Your lucky number is {b.luck}')