简体   繁体   English

范围/命名空间中的语义错误以及python中的返回函数

[英]Semantic error with scope/namespace with return function in python

While trying to understand return and scope I tried writing the function below but found that it only returns the value of x evaluated as x += 20 在尝试了解return和scope时,我尝试在下面编写函数,但发现它仅返回评估为x += 20的x的值

 def this_is_return(x):
    """
    takes as input x and passes it to a second function which operates 
    on it returns to first function which operates on it more before 
    returning value to console
    """
     def second_function(x):
         x *= 2
         print(x)
         return x
     second_function(x)
     x += 20
     return x

When written as below the function works as expected. 如下编写时,该功能将按预期工作。 But I am really interested in what I am not understanding about the limits of return with respect to the initial value x. 但是,我对我对初始值x的回报限制不了解的东西很感兴趣。 My block diagramming skills are nebulous. 我的框图绘制技能很模糊。

 def this_is_return(x):
    """
    takes as input x and passes it to a second function, which 
    operates on it and assigns it to c, returns c to
    function assigned to d; first function then operates on it more 
    before returning value to console
    """
    def second_function(x):
        c = x * 2
        print(c)
        return c
    d = second_function(x)
    x = d + 20
    return x

If I understand your question correctly, the heart of the problem is that a reasonable person might expert that x *= 2 doubles the value of x . 如果我没有理解你的问题,问题的心脏是一个理智的人可能是专家x *= 2双打的价值x It does, but the doubling doesn't persist: 可以,但是不会持续加倍:

>>> def second_function(x):
...          x *= 2
...          print(x)
...          return x
... 
>>> x = 10 ; second_function(x)
20
20
>>> x
10

Let's take this step by step: 让我们逐步进行此操作:

  1. def second_function(x):

    x points to the data that was passed in. x指向传入的数据。

  2. x *= 2

    The data that was passed in was multiplied by two and, locally, x points to a new number that is double the old number. 传入的数据乘以2,在本地, x指向一个新数字,该数字是旧数字的两倍。 The memory location that contains the old number is unaffected. 包含旧数字的存储位置不受影响。

    Integers are immutable. 整数是不可变的。 They cannot be changed "in-place." 它们不能“就地”更改。

  3. print(x); return x

    The local x is printed and returned. 将打印并返回本地x

In the first_function, though, x is still pointing to the original data. 但是,在first_function中, x仍指向原始数据。 The original data was never changed. 原始数据从未更改。

In sum, x *= 2 does not change the data in-place. 总之, x *= 2不会就地更改数据。 It causes x to point to new number. 它使x指向新数字。 Other variables which point to the old data are unaffected. 指向旧数据的其他变量不受影响。

Alternate version using mutable variables 使用可变变量的替代版本

Instead of passing an integer, let's pass in a list: 让我们传递一个列表,而不是传递一个整数:

>>> def third_function(x):
...     x[0] *= 2
...     print(x)
... 
>>> x = [10]; third_function(x)
[20]
>>> x
[20]

As you can see, the change to the list x persists. 如您所见,对列表x的更改仍然存在。 Lists are mutable. 列表是可变的。

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

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