[Twisted-Python] some callback function params problems
Hello, I have some callback function params problems. Here is a simple example. def deferDoSomeInsert(paramA, paramB): insertSQL = "..." db.runQuery(insertSQL) def deferDoSomeDelete(paramA, paramB): deleteSQL = "..." db.runQuery(deleteSQL) #these two functions are individual and basic, and then, I need compound function which first insert then delete. #so def deferCompoundFunc(paramA, paramB): return deferDoSomeInsert.addCallback(deferDoSomeDelete, paramA, paramB) here the problem comes, the first defer will return none as default so there're 3 pramas pass to the second function. Is there a common way to solve the problem like this. I mean I can still keep the separate functions as the look like now, and I can compound the basic function to do more complex action using addCallback to link them. 2009-06-09 hoooooosety
On Tue, Jun 09, 2009 at 05:31:52PM +0800, hoooooosety wrote:
Is there a common way to solve the problem like this. I mean I can still keep the separate functions as the look like now,
I do it like this: def deferCompoundFunc(paramA, paramB): d = deferDoSomeInsert(paramA, paramB) d.addCallback(lambda ignored: deferDoSomeDelete(paramA, paramB)) return d (Actually I normally call the lambda's parameter "_" rather than "ignored" because it's faster to type, and makes pylint not complain about unused variables) If you're learning Twisted for the first time, get prepared to get very friendly with lambda. :)
participants (2)
-
hoooooosety
-
Tim Allen