简体   繁体   English

Python-名称未在函数变量中定义?

[英]Python - Name is not defined in a function variable?

I seem to be somehow screwing up the most basic thing. 我似乎在某种程度上搞砸了最基本的事情。 I have: 我有:

def function(a, b, c):
    return 'hi'

print(function(a,b,c)) results in a NameError for every variable. print(function(a,b,c))对每个变量都会导致NameError

What is the cause of this? 这是什么原因?

The names of the arguments of a function are local variables , they are not available as global names. 函数参数的名称是局部变量 ,不能用作全局名称。 a , b and c exist only inside of the function, and receive the values you pass to the function. abc仅存在于函数内部 ,并接收传递给函数的值。

You need to create new variables or use literal values when calling the function: 调用函数时,您需要创建新变量或使用文字值:

print(function(1, 2, 3))

would work because 1 , 2 and 3 are actual values to pass into the function. 将工作,因为123是通入功能的实际值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM