开发者

struggling with Timer objects in python

开发者 https://www.devze.com 2023-02-24 01:44 出处:网络
again i am struggling with something alth开发者_如何学Goough it might be a little simpler to fix unlike the binary arrays with my last post. Basically, I created a timer object with a function name. Y

again i am struggling with something alth开发者_如何学Goough it might be a little simpler to fix unlike the binary arrays with my last post. Basically, I created a timer object with a function name. Yet, I keep getting a problem because it says that the function that i call is not defined under Name Error.

class DrawBot():
    waitingt = Timer(30.0, lockmap)
    ...
    def onlockmap(self, user):
        self.onBackup(user, "lockmapbackup")
        waitingt.start()

    def lockmap():
        onrestoremap("lockmapbackup")


NameError: name 'lockmap' is not defined


Because it's not defined until you get to the actual definition. Plus, you probably don't want to have a single timer shared across every instance of the class... try this instead:

class DrawBot():
    def __init__(self):
        self.waitingt = Timer(30.0, self.lockmap)
    ...
    def onlockmap(self, user):
        self.onBackup(user, "lockmapbackup")
        self.waitingt.start()

    def lockmap(self):
        onrestoremap("lockmapbackup")


lockmap() is part of DrawBot(), so if you were to call it by itself, you'd get a NameError.

Try calling it by using self, which references the class:

waitingt = Timer(30.0, self.lockmap)
0

精彩评论

暂无评论...
验证码 换一张
取 消