I can use a function's attribute to set a status flag, such as
def Say_Hello():
if Say_Hello.yet == True:
print('I said hello already ...')
elif Say_Hello.yet == False:
Say_Hello.yet = True
print('Hello!')
Say_Hello.yet = False
if __name__ == '__main__':
Say_Hello()
Say_Hello()
and the output is
Hello!
I said hello already ...
However, when trying to put the function in a class, like
class Speaker:
def __init__(self):
pass
def Say_Hello(self):
if self.Say_Hello.yet == True:
print('I said hello already ...')
elif self.Say_Hello.yet == False:
self.Say_Hello.yet = True
print('Hello!')
Say_Hello.yet = False
if __name__ == '__main__':
speaker = Speaker()
speaker.Say_Hello()
speaker.Say_Hello()
There is this error:
Traceback (most recent call last):
File "...func_attribute_test_class_notworking.py", line 16, in <module>
speaker.Say_Hello()
File "...func_attribute_test_class_notworking.py", line 9, in Say_Hello
self.Say_Hello.yet = True
AttributeError: 'method' object has no attribute 'yet'
What is the proper way to use function's attribute in a class?