我工作的一個項目Pygame的時候我遇到了這樣的問題:經與拉姆達的麻煩在Python
# Note: pos is the tuple that stores mouse position
self.start_on = lambda: True if pos[1] >= 100 and pos[1] <= 200 else False
print(self.start_on)
回報
<function Menu.mouseLogic.<locals>.<lambda> at 0x10346d1e0>
,而不是真或假。
我也試過:
self.start_on = (lambda: True if pos[1] >= 100 and pos[1] <= 200 else False)
print(self.start_on)
但返回同樣的事情。
如何讓它返回True或False?
(注:這是關於Python 3.5)
不相關,但是如果你想給一個名稱分配一個lambda(一個匿名函數),只需要使用一個普通的'def'。 – skrx