Why does returning in Interactive Python print to sys.stdout?

后端 未结 5 1457
挽巷
挽巷 2021-01-16 04:18

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

def hi():
    return \'hi\'

If I call it in a Python shell,

         


        
5条回答
  •  清酒与你
    2021-01-16 04:38

    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.

提交回复
热议问题