了解可变变量在不同范围内的行为方式
Understanding how mutable variables behave across different scopes
这里是 Python 的新手。在 Codingbat 上练习使用列表时,我意识到有些行为我不理解关于列表和可变变量的行为方式。下面的示例来自一些 Codingbat 问题来构建我的问题...
在 getManipulatedNewList() 中,newnums 被销毁,因为它超出了范围(这是正确的吗?),所以这就是为什么 bobo 第一次得到 None。
我现在假设确保获得列表的实际副本的唯一方法是在外部函数中创建它——所以深度复制必须发生在操作列表的函数之外,在将使用新列表的同一范围内。正确吗?
然后在下面的最后一个函数中传递了什么(whatIsThisDoing()--我显然正在用参数 "nums" 的第一个和最后一个元素创建一个新列表,我然后 return 立即。Bobo 被分配到 new 列表成功(否则 arr[0] = 999 也会更改 bobo 中的第一个值)。
为什么这样做有效,但 getManipulatedNewList() 会破坏 newnums?
有没有什么方法可以像 returned 匿名列表一样传递 newnums,所以它不会 被销毁?
(或者我在这里误解了什么,请:)
import copy
def getManipulatedNewList(nums):
# Create new list
newnums = copy.deepcopy(nums)
# For example only; assume many, more complex list manipulations
# occur here
newnums = newnums.reverse()
# Return the newly-created list reference (doesn't work.)
return newnums
def manipulateList(nums):
# For example only; assume many, more complex list modifs
# occur here
nums = nums.reverse()
# Nothing to return
def whatIsThisDoing(nums):
# This actually returns something!
# Why, though? It's creating the new list in this function too.
return [nums[0], nums[-1]]
if __name__ == '__main__':
arr = [1,2,3]
print(arr)
print("---")
bobo = getManipulatedNewList(arr)
print(arr) # Shouldn't be touched, as expected
print(bobo) # newnums was destroyed so bobo is None
print("---")
# Is this the only good solution to working with mutable vars?
bobo = copy.deepcopy(arr)
manipulateList(bobo)
print(bobo)
# Why does this work?
bobo = whatIsThisDoing(arr)
print(bobo)
print("---")
arr[0] = 999
print(bobo) # bobo[0] isn't changed, so it's not referencing arr[0]
没有。你得到 None 的原因是因为这是从 reverse
返回的;该方法有效。这个问题与范围无关,您的其他误解都源于此。
这里是 Python 的新手。在 Codingbat 上练习使用列表时,我意识到有些行为我不理解关于列表和可变变量的行为方式。下面的示例来自一些 Codingbat 问题来构建我的问题...
在 getManipulatedNewList() 中,newnums 被销毁,因为它超出了范围(这是正确的吗?),所以这就是为什么 bobo 第一次得到 None。
我现在假设确保获得列表的实际副本的唯一方法是在外部函数中创建它——所以深度复制必须发生在操作列表的函数之外,在将使用新列表的同一范围内。正确吗?
然后在下面的最后一个函数中传递了什么(whatIsThisDoing()--我显然正在用参数 "nums" 的第一个和最后一个元素创建一个新列表,我然后 return 立即。Bobo 被分配到 new 列表成功(否则 arr[0] = 999 也会更改 bobo 中的第一个值)。
为什么这样做有效,但 getManipulatedNewList() 会破坏 newnums?
有没有什么方法可以像 returned 匿名列表一样传递 newnums,所以它不会 被销毁?
(或者我在这里误解了什么,请:)
import copy
def getManipulatedNewList(nums):
# Create new list
newnums = copy.deepcopy(nums)
# For example only; assume many, more complex list manipulations
# occur here
newnums = newnums.reverse()
# Return the newly-created list reference (doesn't work.)
return newnums
def manipulateList(nums):
# For example only; assume many, more complex list modifs
# occur here
nums = nums.reverse()
# Nothing to return
def whatIsThisDoing(nums):
# This actually returns something!
# Why, though? It's creating the new list in this function too.
return [nums[0], nums[-1]]
if __name__ == '__main__':
arr = [1,2,3]
print(arr)
print("---")
bobo = getManipulatedNewList(arr)
print(arr) # Shouldn't be touched, as expected
print(bobo) # newnums was destroyed so bobo is None
print("---")
# Is this the only good solution to working with mutable vars?
bobo = copy.deepcopy(arr)
manipulateList(bobo)
print(bobo)
# Why does this work?
bobo = whatIsThisDoing(arr)
print(bobo)
print("---")
arr[0] = 999
print(bobo) # bobo[0] isn't changed, so it's not referencing arr[0]
没有。你得到 None 的原因是因为这是从 reverse
返回的;该方法有效。这个问题与范围无关,您的其他误解都源于此。