Use return value in another function

Just to fix the terminology and meaning of words:

  • For the demonstrated use, functions do not need to be objects (like all other objects in python). So to avoid confusion it is best to remove the unrelated first part of the sentence.
  • “function nesting” is something different, please see below. In your example you just call function1 and pass its return value as an argument to function2. If we would like to find a fancy name for this we could use something like “chained calling of functions”?[1]

Nested functions:

def function1():
    def function2():
        pass

In the example above function2 is an inner function or a nested function. This concept is useful for example when you are creating decorators.


The common way how to write the print in Python would be using an f-string:

print(f'{item[key[0]]} = {item[key[1]]}')

The advantages which come to my mind:

  • Not limited to print(). We are creating a string.
  • We can start adding formatting (padding etc.) right away.
  • For people used to f-strings this is a single flow of string, not interrupted by commas or + operators.

  1. Like Method chaining - Wikipedia ↩︎

2 Likes