Python Forum
return vs. print in nested function example
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
return vs. print in nested function example
#1
Here's a nested function:

def outer():
    n = 1
    
    def inner():
        n = 2
        print(n)
        #return n
    
    inner()
    return n
If I type outer(), then I get 2 and 1 on separate lines. If I comment out Line 6 instead of 7, then I only get 1 as a single line of output.

Why is this? In the latter case, inner() should return n (2) and inner() is called in the outer function.
Reply
#2
This is similar to your other question. In interactive mode, the result of an expression is printed. But that does not happen by default. Normally bare expressions are evaluated, but any result is just discarded unless printed or assigned. You may want to try running programs from files, not from the interactive prompt.

Yes inner() does evaluate to 2, but that value is not used. Then outer() evaluates to 1. Since you typed it, the interactive shell prints that result for you.
Mark17 likes this post
Reply
#3
(Jan-04-2022, 04:30 PM)bowlofred Wrote: This is similar to your other question. In interactive mode, the result of an expression is printed. But that does not happen by default. Normally bare expressions are evaluated, but any result is just discarded unless printed or assigned. You may want to try running programs from files, not from the interactive prompt.

Yes inner() does evaluate to 2, but that value is not used. Then outer() evaluates to 1. Since you typed it, the interactive shell prints that result for you.

Sorry for the duplication. I guess it's because I'm still confused.

Jupyter Notebook is 100% interactive, correct? I want to continue using it as I've found it very helpful and heard a lot of good things. This seems to be a limitation, though, because it prevents me from seeing the results of true Python (for lack of a better term)?
Reply
#4
Interactive sessions give you a way to view intermediate calculations and expressions. They shouldn't change the result of the program, but they might display some of the data differently.

If you want to view the result from inner(), you can just print() it.

def outer():
    n = 1

    def inner():
        n = 2
        #print(n)
        return n

    print(inner()) #prints the result of inner() or a "2"
    return n

outer() # evaluates to 1, but isn't printed
Mark17 likes this post
Reply
#5
It's just learning the rules of Jupyter.
Make your cell breaks in logical places, where you would like to see the interval result (but not in the middle of a function).
Recognize Jupyter will display the output of the last expression, IF NOT ASSIGNED.
dfA = df
No display
dfA
Dataframe A will be displayed, according to your display rules
Mark17 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 233 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  nested function return MHGhonaim 2 562 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,163 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,129 Feb-04-2023, 12:30 PM
Last Post: caslor
  How to print variables in function? samuelbachorik 3 851 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  How to print the output of a defined function bshoushtarian 4 1,234 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  Why does absence of print command outputs quotes in function? Mark17 2 1,340 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  Exit function from nested function based on user input Turtle 5 2,858 Oct-10-2021, 12:55 AM
Last Post: Turtle
  output correction using print() function afefDXCTN 3 10,960 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  How to invoke a function with return statement in list comprehension? maiya 4 2,746 Jul-17-2021, 04:30 PM
Last Post: maiya

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020