Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 与#x27 ;;Lambda格式';?_Python_Object_Lambda_Argument Passing - Fatal编程技术网

Python 与#x27 ;;Lambda格式';?

Python 与#x27 ;;Lambda格式';?,python,object,lambda,argument-passing,Python,Object,Lambda,Argument Passing,首先,我是Python的乞丐,所以请准备好一个nooby问题 在本网站的一个主题中,我发现了一条关于lambda函数使用的非常有用的建议 以下是更正前的代码: def entree1(self): #function that is supposed to change text in self.configure(text = "X") the label block from whatever it is to 'X' fe

首先,我是Python的乞丐,所以请准备好一个nooby问题

在本网站的一个主题中,我发现了一条关于lambda函数使用的非常有用的建议

以下是更正前的代码:

def entree1(self):                    #function that is supposed to change text in 
self.configure(text = "X")              the label block from whatever it is to 'X'

fen = Tk()

pole1 = Label(fen, text = '|_|')
pole1.bind("<Button-1>", lambda: entree1(pole1))         #event handler reffering to the function above
def entree1(self):                    #function that is supposed to change text in 
self.configure(text = "X")              the label block from whatever it is to 'X'

fen = Tk()

pole1 = Label(fen, text = '|_|')
pole1.bind("<Button-1>", lambda x: entree1(pole1))          #event handler reffering to the function above
def entre1(self):#本应在中更改文本的函数
self.configure(text=“X”)将标签块从任意位置配置为“X”
fen=Tk()
pole1=标签(分,文本='| | |')
pole1.bind(“,lambda:entre1(pole1))#引用上述函数的事件处理程序
更正后,以下是我的代码:

def entree1(self):                    #function that is supposed to change text in 
self.configure(text = "X")              the label block from whatever it is to 'X'

fen = Tk()

pole1 = Label(fen, text = '|_|')
pole1.bind("<Button-1>", lambda: entree1(pole1))         #event handler reffering to the function above
def entree1(self):                    #function that is supposed to change text in 
self.configure(text = "X")              the label block from whatever it is to 'X'

fen = Tk()

pole1 = Label(fen, text = '|_|')
pole1.bind("<Button-1>", lambda x: entree1(pole1))          #event handler reffering to the function above
def entre1(self):#本应在中更改文本的函数
self.configure(text=“X”)将标签块从任意位置配置为“X”
fen=Tk()
pole1=标签(分,文本='| | |')
pole1.bind(“,lambda x:entre1(pole1))#引用上述函数的事件处理程序
简而言之,我将lambda:some func更改为lambda x:some func

虽然我不知道这两种变体之间的区别,但它确实起了作用,这很好。 你能告诉我在我添加了x之后,到底发生了什么变化吗


谢谢你抽出时间

让我将lambda表达式转换为您可能更熟悉的函数定义:

lambda : entree1(pole1)

def lambdaFunc():
    global pole1
    return entree1(pole1)
def lambdaFunc(x):
    global pole1
    return entree1(pole1)
你的修正函数是

lambda x : entree1(pole1)
这和

def lambdaFunc():
    global pole1
    return entree1(pole1)
def lambdaFunc(x):
    global pole1
    return entree1(pole1)

您需要额外的参数,因为Tk按钮使用变量调用绑定到的函数(我完全忘记了变量是什么),因此在不使用输入变量的情况下使用输入变量调用函数会导致错误。

pole1的
global
声明是多余的。啊,好吧,我想我明白了。对我来说,这仍然是一个相当奇怪的概念,但我认为真正的理解会随着时间的推移而到来。非常感谢你!