【发布时间】: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,请阅读此内容,它可能会帮助您理解“Pythonic”的感觉python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
标签: python function python-3.x scope