0

again i am struggling with something although 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

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

self references the instance, but the lookup algorithm would also search the class if the instance doesn't have the name. This really should have an instance of Timer for class instance, not a class level instance.
Ah, thanks. I've never seen the Timer class, so I just thought that it was a generic error...
2

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")

1 Comment

I think you need to use self.lockmap here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.