I am trying to call method within a class; that call is the last line below, self.z()
class Wait:
def __init__(self,a):
self.a = a
def countdown(self,a):
for remaining in range(self.a, 0, -1):
sys.stdout.write("\r")
sys.stdout.write("{:2d} seconds remaining.".format(remaining))
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\rWait Complete! \n")
def z(self):
self.countdown(100)
self.z()
However, I get this error:
Traceback (most recent call last):
File "./countdown.py", line 6, in <module>
class Wait:
File "./countdown.py", line 18, in Wait
self.z()
NameError: name 'self' is not defined
How can I call countdown from another method within this class ?
self.z()immediately upon object creation?self.z()that works only from within an instance method of the class. what do you want to achieve?countdownas a standalone function?self.countdown(some_number), exactly as you have done. There's nothing wrong withcountdownin your code; the problem is that you're callingz()in a place where you're not allowed to. Presumably in your real code, this won't be a problem.