我有一个非常简单但也许很愚蠢的问题:
为什么会这样?
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/