[英]Variable referenced before assignment in Python function
Can someone please tell me what's wrong with the following Python code.有人可以告诉我以下 Python 代码有什么问题。
def education():
class College:
type = "University"
def __init__(self, name, location):
self.name = name
self.location = location
iet = College("IET", "Kanpur")
jim = College("JIM", "Lucknow")
edu1 = print("IET is a {}".format(iet.__class__.type))
edu2 = print("JIM is also a {}".format(jim.__class__.type))
loc1 = print("{} is located in {}".format(iet.name, iet.location))
loc2 = print("{} is located in {}".format(jim.name, jim.location))
return edu1, edu2, loc1, loc2
education()
NameError: free variable 'College' referenced before assignment in enclosing scope
I get the above NameError while calling the function.我在调用 function 时收到上述 NameError。
Thanks in advance.提前致谢。
You are trying to create College()
objects inside the class College
, but outside any function in that class.您正在尝试在 class
College
内创建College()
对象,但在 class 中的任何 function 之外创建对象。 That is what's causing the error.这就是导致错误的原因。
If you unindent the part after __init__
, it will work, like so:如果您在
__init__
之后取消缩进该部分,它将起作用,如下所示:
def education():
class College:
type = "University"
def __init__(self, name, location):
self.name = name
self.location = location
iet = College("IET", "Kanpur")
jim = College("JIM", "Lucknow")
edu1 = print("IET is a {}".format(iet.__class__.type))
edu2 = print("JIM is also a {}".format(jim.__class__.type))
loc1 = print("{} is located in {}".format(iet.name, iet.location))
loc2 = print("{} is located in {}".format(jim.name, jim.location))
return edu1, edu2, loc1, loc2
education()
However, I would advise to define the class College before the function, like so:但是,我建议在 function 之前定义 class 学院,如下所示:
class College:
type = "University"
def __init__(self, name, location):
self.name = name
self.location = location
def education():
iet = College("IET", "Kanpur")
jim = College("JIM", "Lucknow")
edu1 = print("IET is a {}".format(iet.__class__.type))
edu2 = print("JIM is also a {}".format(jim.__class__.type))
loc1 = print("{} is located in {}".format(iet.name, iet.location))
loc2 = print("{} is located in {}".format(jim.name, jim.location))
return edu1, edu2, loc1, loc2
education()
Edit: You're declaring type
in class College
, thereby hiding Python's type
.编辑:您在 class
College
中声明type
,从而隐藏 Python 的type
。 It is not advised to use names for variables that are default keywords in Python, such as list, dict, type
.不建议使用 Python 中默认关键字的变量名称,例如
list, dict, type
。
Edit2: As Ishwar pointed out, you're storing the return from print
statements in loc1
and loc2
. Edit2:正如 Ishwar 指出的那样,您将
print
语句的返回值存储在loc1
和loc2
中。 print()
always returns None
, so there is no use in storing it in variables. print()
始终返回None
,因此将其存储在变量中是没有用的。 If you wanted to store the formatted strings, store those in variables and then print those variables.如果您想存储格式化的字符串,请将它们存储在变量中,然后打印这些变量。
Python uses indentation to determine if code is part of the function or part of the class. Python 使用缩进来确定代码是 function 的一部分还是 class 的一部分。 Here your code referencing
College
is indented such that it forms part of the class and not your function.在这里,您引用
College
的代码是缩进的,这样它 forms 是 class 的一部分,而不是您的 function。
Additionally, you are defining you class inside your function.此外,您在 function 中定义了 class。 I can see no good reason why you should do this.
我看不出你应该这样做的充分理由。 Also your variable
type
is a reserved keyword, so its advised to change this to something else, such as college_type
.此外,您的变量
type
是保留关键字,因此建议将其更改为其他内容,例如college_type
。 Lastly the print statement returns None, so none of your variables will be populated with any data.最后,打印语句返回 None,因此您的任何变量都不会填充任何数据。
See the code below:请看下面的代码:
class College:
college_type = "University"
def __init__(self, name, location):
self.name = name
self.location = location
def education():
iet = College("IET", "Kanpur")
jim = College("JIM", "Lucknow")
edu1 = iet.__class__.college_type
edu2 = jim.__class__.college_type
print("IET is a {}".format(edu1))
print("JIM is also a {}".format(edu2))
loc1 = iet.location
loc2 = jim.location
print("{} is located in {}".format(iet.name, loc1))
print("{} is located in {}".format(jim.name, loc2))
return edu1, edu2, loc1, loc2
education()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.