我是靠谱客的博主 洁净溪流,这篇文章主要介绍python定义全局列表_Python:修改函数内的全局列表,现在分享给大家,希望可以做个参考。

First of all, I understand that I can use global statement to access global variables. But somehow I was able to modify a global list without global like below:

def func1(nums):

nums = [4,5,6]

nums = [1,2,3]

func1(nums)

print nums # print [1,2,3]

def func2(nums):

nums[0] = 4

nums[1] = 5

nums[2] = 6

nums = [1,2,3]

func2(nums)

print nums # print [4,5,6]

After trying func2, I realized that I can always access global list in a function if I specify the index:

def func3(nums):

nums[:] = [4,5,6]

nums = [1,2,3]

func3(nums)

print nums # print [4,5,6]

Is it because Python automatically go trying to match a global variable if a function variable is used before definition?

Thanks.

解决方案I understand th

最后

以上就是洁净溪流最近收集整理的关于python定义全局列表_Python:修改函数内的全局列表的全部内容,更多相关python定义全局列表_Python:修改函数内内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部