【问题标题】:I'm lost and confused! regarding functions and scope of returned objects. Why is the object returned from the first function changed?我很迷茫和迷茫!关于返回对象的功能和范围。为什么从第一个函数返回的对象改变了?
【发布时间】:2017-01-28 19:23:00
【问题描述】:

我是编程和自学 Python 的新手。

我创建了两个函数。第一个返回一个对象A。第二个接受 objectA 作为参数。如何在函数之间保持对象“活动”?

listA = ""  # a string
print(type(listA))

def one():
    listA = [1,3,2,4]   # within this function, listA is now a list object
    print (type(listA))
    print ("def one:", (listA))
    return listA  # returning a list object called listA

def two(listA):  # def two() recieves object "listA" but its no longer a list object

    print(type(listA))  # why is listA not a list object?
    print("def two:", (listA))


one()
two(listA)

【问题讨论】:

标签: python function python-3.x scope


【解决方案1】:

这就像问“我家的墙上有一张照片,当我去朋友家时,他们也有一张照片,但不一样。为什么不一样?如果他们是picture 不应该是一样的吗?”。

然后你走到外面看到另一张照片,它又不一样了。

这就是scope 的含义 - 函数中的代码与其他代码分组/隔离/分离,该代码中的变量名称与该代码之外的变量名称不同。 name 在不同的地方可能相同,但这并不意味着它们完全一样。

你的代码实际上在做什么:

# --- 'outside world' scope at this level

worldListA = ""  # a string
print(type(worldListA))

def one():
    # --- function scope starts
    oneListA = [1,3,2,4]   # within this function, oneListA is now a list object
    print (type(oneListA))
    print ("def one:", (oneListA))
    return oneListA  # returning whatever "oneListA" means in this scope region
    # --- function scope ends


def two(twoListA):  # def two() recieves /anything/ and gives it the name "twoListA" inside this /scope/ region
    # --- function scope starts   
    print(type(twoListA))  # why is twoListA not a list object?
    print("def two:", (twoListA))
    # --- function scope ends


# --- 'outside world' scope again at this level

# this function call one() is going to return /something/
# and we're not catching what comes out of it
# so it's not going to go anywhere and will be thrown away
one()

# call the function two() and pass in the thing called "worldListA" from 
# the world scope
# that content will become named as 'twoListA' inside the two() function
two(worldListA)


# what you expected to happen was this code:

worldListA = one()
two(worldListA)

注意我已经更改了变量名和 cmets。

然后是的,您可以将所有作用域中的所有变量更改为具有完全相同的名称 -> 但这不会使它们成为相同的变量

注意。如果你的房子没有picture,你可以透过窗户看到外面的世界。如果您的函数没有同名变量,Python 将尝试从下一级外部范围获取一个。

【讨论】:

    【解决方案2】:

    这是范围问题。在one() 的范围内,您更改了listA 的类型。但在全局范围内(one() 之外),listA 仍然是一个字符串。

    返回新的listA 不足以改变这一点。您必须将值分配给在全局范围内定义的变量,可能像这样:

    listA = one()
    two(listA)
    

    更多信息:http://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html

    【讨论】:

      【解决方案3】:

      您对使用名称listA 表示三个完全独立的变量感到困惑。文件顶部声明的全局变量、one 内部的局部变量和two 的参数完全没有关系

      在函数之间保留变量的方法是捕获结果,然后将其传递给下一个。目前发生的情况是您从one 返回数据,然后将其丢弃;全局变量没有被修改。

      你需要做的:

      new_listA = one()
      two(new_listA)
      

      【讨论】:

        【解决方案4】:

        您可以使用global 关键字指向全局范围内的listA。

        所以在函数one 你说global listA

        【讨论】:

          猜你喜欢
          • 2015-07-25
          • 1970-01-01
          • 2023-03-04
          • 2016-02-14
          • 2021-11-01
          • 1970-01-01
          • 1970-01-01
          • 2022-09-24
          • 1970-01-01
          相关资源
          最近更新 更多