开发者

Call function in function arguments

开发者 https://www.devze.com 2022-12-27 14:00 出处:网络
I have a function, we\'ll call it funcA that returns an object. I want to pass that object to funcB. Can I do this?

I have a function, we'll call it funcA that returns an object. I want to pass that object to funcB. Can I do this?

funcB(funcA());

so that funcA() is called fi开发者_如何学Pythonrst and the results are passed to funcB()?


Yes, yes you can.


If it helps, Yisroel, you can think of the object returned "replaces" the function in-place. So, after funcA() is executed, it is "replaced" by the returned value and passed into funcB().

Generally, though, you wouldn't want to do nesting functions like this for more than 3 levels. If it starts to become unreadable, store the object into a variable and passing this variable into the next function can make your code much more readable.

So the answer is yes, as everyone else pointed out.


Yes. The parameters to the function are expressions which are evaluated before the expression value is passed to the function. An expression can of course be a function call.


Yes, this should work as you want. funcB(funcA()) calls funcA() and passes the result to funcB()

Perhaps you were concerned that it would pass the function funcA to funcB()? For that, the syntax would be funcB(funcA)


I don't see any problem with this, have you tried any sample?

It should work fine.

If you have faced any specific issue please post it.

If you want to pass the funcA as a parameter to funB then you have to call funcB as funcB(funcA)

0

精彩评论

暂无评论...
验证码 换一张
取 消