Connecting to a function with arguments

后端 未结 1 1048
-上瘾入骨i
-上瘾入骨i 2021-01-21 20:19

I\'m working on a user interface with PyQt and have finally figured out how to make multiple display options possible (user clicks on a radio button, a signal is saved, and depe

相关标签:
1条回答
  • 2021-01-21 20:42

    You say:

    The interesting thing is that the specified functions (function1 and function2) appear to run even though I get the error message.

    Nothing surprising about that -- you're calling one of those functions right here, and passing that function's result (no doubt the None you get error messaged about) to connect -- that's always, in Python, the meaning of

    a(b(c))
    

    for any callables b and a: it means "call b with argument c and pass its result as the single argument of a.

    Now when you use, instead,

    a(lambda: b(c))
    

    you're asking for a 100%-different semantics -- passing as a's single argument a function that, when later called (w/o arguments), will then "call b with argument c".

    functools.partial(b, c), by the way, is an arguably more elegant approach then lambda -- but produces exactly the same semantics.

    As for why this isn't fixing everything for you -- I don't know: the code you show, amended with lambda, should be fine -- if everything else in the vast amount of code you don't show was absolutely perfect. So I suspect the latter condition doesn't hold. Can you make a simplified-to-the-bone but complete example that exhibits the bug you still observe (once the lambda fix is applied)...?

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