Print calls, and their returns

I made this little code for the forum peeps to see how the ‘print’ calls work.
I’ve seen a few struggles with it, and thought this little diddy might help.

python 3 print() is a function/method like any other method/function. It takes what you put in as ‘args’ and looks for the value of those ‘args’ and prints them. It will even print a blank newline if there are no ‘arguments’ in the function call. I couldn’t find anything right off hand that you couldn’t put in this print() function.

When a function has no return value, lets say, something like:

def add(a, b):
    x = a + b

instead of:

def add(a, b):
    x = a + b
    return x

The value of that first add function is ‘None’. It’s performing an action, but it is local, and not returning any value. A function can do lots of things but if you want to assign any value to a function it has to have a return value.

Print is an action. It will not modify any args you give it, but it will go through the steps to ‘sys.stdout’ what arguements you give it.

In the code below, see what is being returned, and what is being printed. The print function is writing a value to the command prompt for you, but the functions themselves are calling print to action. The last function prints as ‘None’ because that function has no return value, it is a None.

def function_N():
    print('Nellie')
    return function_I()

def function_I():
    print(' is ')
    return function_A()
    
def function_A():
    print('Awesome')
    return function_None()

def function_None():
    print("The next line will be None")

print(function_N())

And this code can show you how the order of function call’s are made.
You could modify the value of that initial return value in each argument, and only the end result will print {be written to the prompt} because an order of operations is carried out. Parenthesis first, from innermost to outermost. The value of call_3 is the value of call_2 is the value of call_1. It has to process call_1 before it can go back through the chain to calculate the second call, and then to process the third call, it needs to process the call_2 value.
The reason only the final value is printed, is because of the order of operations. Print() is the final function in that order, and therefore only prints the value determined by the calls made inside it.
Imagine it like mathmatical operations:
2 * (( 1 + 2 * ( 8 - 6) ) / 4).
The innermost parenthesis are processed first.


def call_1():
    return '1'

def call_2(arg):
    return arg

def call_3(arg):
    return arg


print(call_3(call_2(call_1())))

One last bit, This is probably what the ‘print’ function looks like:
the arg has to be converted to a string for sys.stdout to work.


def sysout(arg):
    arg = str(arg)
    
    sys.stdout.write(arg)

results:

1 Like

Can you mention where are you getting problems?

1 Like

You should explain why this does what it does.

1 Like

Ok, i will Edit tomorrow to add the explanation.
:slight_smile: