0

I am having trouble with an error which should not be possible.. Here is my situation :

I am developping a IHM in pyside2, so I am using widgets with signals like buttons.

I want to use a method inside an other method and all this is inside a for loop. So I am using the lambda method. Except this time something strange happen. When I want to call a method of myObject inside the lambda, I have the error int object has no attribute myMethod but the method does exist and myObject is not an int !

This is a simple example of what I am doing :

myOtherObject = myOtherClass()
for i in range(10):
    myObject = myClass(i)
    myOtherObject.myOtherMethod.connect(lambda obj=myObject: obj.myMethod())

So I investigated this error and when I print the variable obj inside the lambda, it returns to me "0 or 2" everytime I am doing my signal, which is an On/Off button by the way.

  • Button clicked On => obj = 2
  • Button clicked Off => obj = 0

Any idea ?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    please provide a [mre] – eyllanesc Aug 27 '21 at 08:04
  • 2
    It's impossible to know based on this code alone. A first guess would be that `connect` expects a lambda that it will supply an argument for, thus ignoring your default value. We need to see how the lambda is called. – Kemp Aug 27 '21 at 08:16
  • 1
    There's no MWE (or put plainly, we can't paste code and try it ourselves), not even the code where the method is called, so I can't say this for sure. But your lambdas will cause the exception you mentioned if you call it with 1 argument that is an `int`, not a `myClass`. – BatWannaBe Aug 27 '21 at 08:39
  • Try instead of a lambda using a normal named function, then you can debug/print it's parameters and return value – DisappointedByUnaccountableMod Aug 27 '21 at 10:27
  • use `pathToggle.stateChanged.connect(lambda state, pathConf=pathConfig, check=pathToggleCheck: pathConf.checkToggle(check))` – eyllanesc Aug 27 '21 at 10:36
  • @eyllanesc It's working !!! Thanks :) – Geoffrey Aug 27 '21 at 11:46

0 Answers0