Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass a function call as a parameter to another function? Python

Tags:

python

Let's assume that I am making a function call and pass its return value as a parameter to another function. Should I do it like this:

value = self.__myFunction(self.parent.connectToVars.getCertainValue())

Or like this?

certainValue = self.parent.connectToVars.getCertainValue()
value = self.__myFunction(certainValue) 

Personally, I tend to use the first choice as it is only 1 line of code and I am not making any local variables that could potentially distract me, but I am concerned if there could be a runtime error or it is just a bad practice.

like image 961
magic_turtle Avatar asked Apr 01 '26 11:04

magic_turtle


1 Answers

The first option is perfectly fine, just keep in mind that this way, the return value of the inner function is not accessible later. Therefore, if the inner function is time-costly an You want to use it's return value multiple times, it is better to save it in temporary variable so You don't have to compute it every time from the beginning.

edit:

As Alex mentioned it's usually a bad practice to needlessly compute something multiple times. So summarizing, if You will use a return value of a function multiple times use a temp variable for it, otherwise You can use the first option.

like image 84
Tony Babarino Avatar answered Apr 03 '26 00:04

Tony Babarino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!