Why does returning in Interactive Python print to

2020-03-31 02:43发布

I ran into something different today. Consider this simple function:

def hi():
    return 'hi'

If I call it in a Python shell,

>>> hi()
'hi'
>>> print hi()
hi

It prints out the 'returned' value, even if it's just the repr. This felt odd to me, how could returning be printing to stdout? So I changed it to a script to be run:

def hi():
    return 'hi'
hi()

I ran this from terminal:

Last login: Mon Jun  1 23:21:25 on ttys000
imac:~ zinedine$ cd documents
imac:documents zinedine$ python hello.py
imac:documents zinedine$ 

Seemingly, there's no output. Then, I started thinking this is an Idle thing, so I tried this:

Last login: Tue Jun  2 13:07:19 on ttys000
imac:~ zinedine$ cd documents
imac:documents zinedine$ idle -r hello.py

And here is what shows in Idle:

Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> 
>>> 

So returning prints only in an interactive python shell. Is this a feature? Is this supposed to happen? What are the benefits of this?

5条回答
smile是对你的礼貌
2楼-- · 2020-03-31 02:52

The interactive interpreter will print whatever is returned by the expression you type and execute, as a way to make testing and debugging convenient.

>>> 5
5
>>> 42
42
>>> 'hello'
'hello'
>>> (lambda : 'hello')()
'hello'
>>> def f():
...     print 'this is printed'
...     return 'this is returned, and printed by the interpreter'
...
>>> f()
this is printed
'this is returned, and printed by the interpreter'
>>> None
>>>

See Read–eval–print loop on Wikipedia for more information about this.

查看更多
放我归山
3楼-- · 2020-03-31 02:52

Most interactive shells use a REPL loop - read-eval-print.

They read your input. They evaluate it. And they print the result.

Non-function examples are (using the ipython shell):

In [135]: 3+3
Out[135]: 6    # result of a math operation

In [136]: x=3    # no result from an assignment

In [137]: x
Out[137]: 3    # evaluate the variable, and print the result - its value
查看更多
冷血范
4楼-- · 2020-03-31 03:05

It is a feature of the interactive shell. Yes, it is supposed to happen. The benefit is that it makes interactive development more convenient.

查看更多
对你真心纯属浪费
5楼-- · 2020-03-31 03:11

In Python's interactive mode, expressions that evaluate to some value have their repr() (representation) printed. This so you can do:

4 + 4

Instead of having to do:

print(4 + 4)

The exception is when an expression evaluates to None. This isn't printed.

Your function call is an expression, and it evaluates to the return value of the function, which isn't None, so it is printed.

Interestingly, this doesn't apply to just the last value evaluated! Any statement that consists of an expression that evaluates to some (non-None) value will print that value. Even in a loop:

for x in range(5): x

Different Python command lines can handle this in different ways; this is what the standard Python shell does.

查看更多
【Aperson】
6楼-- · 2020-03-31 03:15

First of all, its not returnning, and it's not related to functions. You just have an expression that evaluates to an object (big surprise! everything in Python is an object).

In this case, an interpreter can choose what to display. The interpreter you're using apparently uses __repr__. If you'd use IPython, you'd see there's a whole protocol, depending on the frontend, determining what will be displayed.

查看更多
登录 后发表回答