0

I have this code I'd like to condense if possible:

def ForgetPassword():
    """ Clear sudo password for extreme caution """
    global SUDO_PASSWORD
    SUDO_PASSWORD = None

self.tools_menu.add_command(label="Forget sudo password", underline=0,
                            font=g.FONT, command=ForgetPassword, state=tk.DISABLED)

Rather than using command=ForgetPassword, is it possible for something like command=SUDO_PASSWORD=NONE?

4
  • No. The command= option is a function to be called later, and you have no option other than a full def here because a lambda cannot assign to a global variable. Commented Dec 3 at 23:29
  • 1
    Like jasonharper said, you need a full def for this but there are workarounds. A terrible approach would be to make SUDO_PASSWORD into a mutable object like a list containing only 1 string. Then you can do something like command=SUDO_PASSWORD_LIST.clear since list objects have a clear method.
    – TheLizzard
    Commented Dec 4 at 2:15
  • @TheLizzard I've used lists before to share variables between inner functions. SUDO_PASSWORD being a list would allow you to put the expiry time as the second value. Commented Dec 4 at 13:21
  • 2
    Just use a function. It’s a couple of lines of code, makes your code more understandable, and also makes it easier to test and debug. Functions are tools to make your job easier. Use them. Commented Dec 6 at 1:49

0

Your Answer

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

Browse other questions tagged or ask your own question.