I wanted to use object-oriented programming in evolution simulation, so I wrote a memo. About changing instance variables by method
sample.py
class SampleClass:
def __init__(self):
self.internal_state = True #Internal state(internal_state)Set to True
def off_internal_state(self): #Method to set internal state to False
self.internal_state = False
def on_internal_state(self): #Method to set internal state to True
self.internal_state = True
def change_internal_state(self): #Method to change internal state
if self.internal_state: #False if true, True if False
self.internal_state = False
else:
self.internal_state = True
You now have a SampleClass class with an internal state. Let's look at the output using a method.
sample.py
sampleObj = SampleClass()
print(sampleObj.internal_state) #=>True
sampleObj.off_internal_state()
print(sampleObj.internal_state) #=>False
sampleObj.change_internal_state()
print(sampleObj.internal_state) #=>True
that's all.
Recommended Posts