简体   繁体   English

函数应该在 Python 中返回打印吗?

[英]Should function return print in Python?

I'm writing function which prepare data in csv file.我正在编写在 csv 文件中准备数据的函数。 I wonder what should it return.我想知道它应该返回什么。 I thought that information in string for user will be good idea, but how should I do it?我认为用户的字符串信息是个好主意,但我该怎么做?

return print('Some info')

Or just要不就

return 'Some info'

And how about exceptions, I mean like above.例外情况如何,我的意思是如上所述。 When exception will end work of function should I return print('Some info') or just 'Some info'?当异常将结束函数的工作时,我应该返回 print('Some info') 还是只返回'Some info'?

No, return should return the result from the function, not the value of print (which is always None anyway).不, return应该返回函数的结果,而不是print的值(无论如何它总是None )。

Usually, a function should not print anything at all.通常,函数根本不应该打印任何内容。 In order to make programs modular and reusable, you want to keep any user interaction in the calling code.为了使程序模块化和可重用,您希望在调用代码中保留任何用户交互。

For example,例如,

def fibonacci(n):
    fib = some calculation ...
    print(fib)
    return fib

fibonacci(33)

This function has the side effect of printing the calculated value.此函数具有打印计算值的副作用。 But this means that you cannot calculate the value without also printing it.但这意味着您不能在不打印的情况下计算该值。 A common design principle from functional programming is that functions should not have side effects anyway.函数式编程的一个常见设计原则是函数无论如何都不应该有副作用。 A better design is更好的设计是

def fibonacci(n):
    fib = some calculation ...
    return fib

print(fibonacci(33))

Exceptions are for situations where the code cannot perform the requested function.代码无法执行请求的功能的情况除外。 For example, you cannot calculate a negative Fibonacci number:例如,您不能计算负的斐波那契数:

def fibonacci(n):
    if n < 0:
        raise ValueError('Cannot calculate negative Fibonacci number')
    fib = some calculation ...
    return fib

You could call this on arbitrary user input;您可以在任意用户输入上调用它;

while True:
    number = input('Give me a number: ')
    try:
        print('Fibonacci: ', fibonacci(int(number))
    except ValueError as e:
        print('Oops, try again; ', e)

Notice how the except actually handles multiple error scenarios: if the use input isn't a number at all, int(number) will also raise a ValueError exception.注意except实际上如何处理多个错误场景:如果 use 输入根本不是数字, int(number)也会引发ValueError异常。

Sounds like you're learning Python for the first time.听起来您是第一次学习 Python。 I recommend reading documentation to understand how I/O and function return statements work:我建议阅读文档以了解 I/O 和函数返回语句的工作原理:

https://docs.python.org/3/tutorial/inputoutput.html https://docs.python.org/3/tutorial/inputoutput.html

https://www.w3schools.com/python/python_functions.asp https://www.w3schools.com/python/python_functions.asp

If you want to return a string in a function, use return 'Some Info' .如果要在函数中返回字符串,请使用return 'Some Info' For printing something to the console for the user to view, simply use print('Some Info') .要将某些内容打印到控制台以供用户查看,只需使用print('Some Info') Combining the two seems pretty useless for your purposes.将两者结合起来似乎对您的目的毫无用处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM