python - 为什么可以在使用变量定义函数后定义变量?

标签 python list function data-structures arguments

我有一个非常简单但也许很愚蠢的问题:

为什么会这样?

def print_list():
    for student in student_list:
        print(student)

student_list = ["Simon", "Mal", "River", "Zoe", "Jane", "Kaylee", "Hoban"]
print_list()

我了解函数和参数的方式,函数 print_list() 不应该识别 student_list 因为我没有将它指定为参数函数。

最佳答案

当您调用 print_list() 时,您已将 student_list 定义为全局变量。

关于python - 为什么可以在使用变量定义函数后定义变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8393122/

相关文章:

python - PyQt4 和 64 位 python

python - `QImage` 构造函数具有未知关键字 `data`

python 3 : Flattening nested dictionaries and lists within dictionaries

c++ - 如何将迭代器类型转换为其原始结构

c++ - 安全获取 &std 列表项?

python - 通过Kivy Button调用不同类中的函数

python - 在 Python 中计算/验证 bz2 (bzip2) CRC32

c++ - 从函数返回对象

php header 位置与 php_redirect

python 解析 xml 字符串