Reputation:
Why I can't make nonlocal variable in Class.
here is code --->
from tkinter import *
class Note:
root = Tk()
nonlocal font_size = 16
def bigger(event):
font_size+=5
root.bind("<Shift-Up>", bigger)
root.mainloop()
output --->
nonlocal font_size = 16
^
SyntaxError: invalid syntax
Upvotes: 1
Views: 261
Reputation: 44838
Yes, nonlocal variable = value
is not valid syntax. nonlocal
, just like global
, is used to "mark" names as nonlocal and global, respectively. It's not a special form of variable definition. You can "mark" a name as nonlocal like this:
nonlocal variable
And then use variable
somewhere in your code.
Upvotes: 3