Python Tkinter Tic Tac Toe With lambda
The only purpose of a lambda
is brevity. This code:
board = [[Button(root, text = "-", font = myFont, command = (lambda x=x, y=y: update(x,y))) for y in range(3)] for x in range(3)]
is the same as this code:
board = []for x in range(3): row = [] for y in range(3): def fn(x=x, y=y): update(x, y) row.append(Button(root, text = "-", font = myFont, command = fn) board.append(row)
Obviously the first form is briefer -- the lambda
is useful because it allows you to define a function as part of a single expression, rather than having to write out a def
block. fn
in the second form is exactly equivalent to lambda x=x, y=y: update(x, y)
in the first form.