简体   繁体   English

在 Python 3.0 中,函数返回 none 是因为函数不存储它的输出,除非我们使用 return 语句?

[英]In Python 3.0 a function returns none because a function does not store it’s output unless we use a return statement?

In Python, does a function just execute it's code block & not store it unless we use a return statement?在 Python 中,除非我们使用 return 语句,否则函数是否只执行它的代码块而不存储它?

When we print variables & expressions I understand we are printing values.当我们打印变量和表达式时,我知道我们正在打印值。

So I am thinking that a function performs it's code block & then does not save that result unless we return it?所以我在想一个函数执行它的代码块然后不保存该结果,除非我们返回它? Is this what's happening in the computer?这是电脑里发生的事情吗?

 Example 1  
def add(a,b):
 nums = a + b

print(add(2,4)+2)
Error

But when we use the return value statement it works但是当我们使用返回值语句时它可以工作

 Example 2
def add(a,b):
 nums = a + b
 return nums

print(add(2,4) + 2)
Output: 8

The error was caused in the first example because the function just executed it's code block & did not save the result therefore resulting in an error due to not being able to add None to an integer(2)?该错误是在第一个示例中引起的,因为该函数刚刚执行了它的代码块并且没有保存结果,因此由于无法将 None 添加到整数(2)而导致错误? & It worked in example 2 because we saved the functions result with the return statement giving us an integer; & 它在示例 2 中有效,因为我们使用返回语句保存了函数结果,返回语句为我们提供了一个整数; Therefore allowing the print statement to print the result of the functions integer + the integer we added it to in the expression?因此允许 print 语句打印函数整数 + 我们在表达式中添加的整数的结果?

In python, functions are blocks of code that execute some logic of some sort (sometimes based on arguments passed into them and sometimes not).在 python 中,函数是执行某种逻辑的代码块(有时基于传递给它们的参数,有时不基于)。 They are very broad and can do many different kinds of things depending on how they are constructed.它们非常广泛,可以根据它们的构造方式做许多不同类型的事情。 I'm not exactly sure what you mean by "store the results" but hopefully some of the following explanation will help.我不确定您所说的“存储结果”是什么意思,但希望下面的一些解释会有所帮助。
All variables created in a function are stored with the "local" scope, meaning that they are only existent when the function is running and are deleted the moment the function terminates.在函数中创建的所有变量都存储在“本地”范围内,这意味着它们仅在函数运行时存在,并在函数终止时被删除。 For example, in the following code, you cannot access the variable x after the function terminates:例如,在以下代码中,您无法在函数终止后访问变量x

def example():
    x = 'Hello World'
    print(x) #This prints: Hello World
example()
print(x) #This will give you a Reference error 

If that is what you mean by "stores the results" then you are right: those results will not be stored.如果这就是您所说的“存储结果”,那么您是对的:这些结果将不会被存储。 You can, however, declare a variable inside of a function to be a global variable, meaning that it can be accessed outside of the function too:但是,您可以将函数内部的变量声明为全局变量,这意味着它也可以在函数外部访问:

def example():
    global x = 'Hello World'
    print(x) #This prints: Hello World
example()
print(x) #This prints: Hello World

When you use the return statement in a function you are just telling the compiler that if a variable is set equal to a function call of said function, whatever follows the return statement is what that variable should be set equal to.当您在函数中使用return语句时,您只是告诉编译器,如果将变量设置为等于所述函数的函数调用,则 return 语句后面的内容就是该变量应设置为等于的值。 However, simply returning a value does not "store" it.但是,简单地返回一个值并不会“存储”它。 See the following code:请参阅以下代码:

def example():
    x = 'Hello World'
    return x
example()
print(x) #This will still cause a reference error
x = example()
print(x) #This prints: Hello World

One final thing to note about the code above: as long as two variables are in different scopes, they can have the same name and not cause an error.关于上面的代码需要注意的最后一件事:只要两个变量在不同的范围内,它们可以具有相同的名称并且不会导致错误。 The x inside the function is in a local scope and the x outside of the function is in the global scope which is why that does not cause an error.函数内部的x在本地范围内,而函数外部的x在全局范围内,这就是为什么不会导致错误的原因。

Welcome to Stack Overflow.欢迎来到堆栈溢出。 When I was learning programming, it helped me to think of calls to functions using an analogy to variables in math.当我学习编程时,它帮助我想到了使用数学中的变量类比来调用函数。 In most languages, you can think of "substituting" the return value in for the function call, the same way you can substitute a literal number into a variable.在大多数语言中,您可以考虑将return值“替换”为函数调用,就像您可以将文字数字替换为变量一样。

In math, you can do this:在数学中,您可以这样做:

m = 4
b = 2

y = m * x + b  # Plug 4 and 2 in for "m" and "b"
y = 4 * x + 2

It's the same with value-returning functions:与返回值函数相同:

def foo():
    return 'bar'
>>> x = foo() # Plug the return value 'bar' in for "foo()"
>>> x
'bar'

In Python, when a function has no explicit return , the default return value is None .在 Python 中,当函数没有显式return时,默认返回值为None So:所以:

def foo():
    print('bar')
    # No return, so Python implicitly returns None
>>> x = foo() # Plug the return value None in for "foo()"
'bar'
>>> x
None

the function define local variable even same name as global variable so when it executed if you don't return something or store the result in global variable the result not appears outside function example该函数定义局部变量,甚至与全局变量同名,因此当它执行时,如果您不返回某些内容或将结果存储在全局变量中,则结果不会出现在函数示例之外

x = 10
def test():
   x= 15
test()
print(x) # result 10

but if use global keyword you can access to global variable like this但是如果使用 global 关键字,您可以像这样访问全局变量

x = 10
def test():
   global x
   x= 15
test() 
print(x) #result 15

or if you return the value或者如果您返回值

x = 10
def test():
  x= 15
  return x
x = test()
print(x) #result 15

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

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