Lambda evaluating unexpectedly

后端 未结 1 1815
故里飘歌
故里飘歌 2021-01-22 00:14

first post here.

I am trying to make a list of movies. The movies will be shown in a list on Tkinter. The plan was when you click an update button the program will updat

1条回答
  •  太阳男子
    2021-01-22 01:01

    Use this instead:

    lambda x=x: self.update_rating(x)
    

    Demonstration:

    >>> x = 1
    >>> a = lambda x=x:x
    >>> a()
    1
    >>> x = 2
    >>> a()
    1
    

    The idea here is that default arguments are evaluated when a function is defined as opposed to creating a closure which evaluates picks up the variables from the enclosing scope when the function is called.

    0 讨论(0)
提交回复
热议问题