简体   繁体   English

Class Name is not defined 错误 python

[英]Class Name is not defined error in python

while i doing my python programming came across an error.在我进行 python 编程时遇到了一个错误。 code is given below.代码如下。

import skfuzzy as fuzz
from skfuzzy import control as ctrl
f =fuzzy_()
print(f)
class fuzzy_:
    def __init__(self):
        pass
    def values(self,t = None, m = None, p = None):

"the class name fuzzy_is not defined". “class 名称模糊_未定义”。 Error shown.显示错误。

Please help.请帮忙。

You need to reorder it like this:您需要像这样重新排序:

import skfuzzy as fuzz
from skfuzzy import control as ctrl

class fuzzy_:
    def __init__(self):
        pass
    def values(self,t = None, m = None, p = None):

# First define the class
# Then instantiate it
f = fuzzy_()
print(f)

The python interpreter needs to find the class definition first while reading in the code from top-down. python 解释器需要先找到 class 定义,同时从上到下读取代码。 In your code example, the interpreter tried instantiating fuzzy_ class object, but threw error as till then it had no idea of what fuzzy_() is.在您的代码示例中,解释器尝试实例化fuzzy_ _class object,但在此之前它不知道fuzzy_()是什么而引发错误。

python interpreter reads from top to bottom, you are calling the instance of class before defining the class, move line 3 and 4 to the bottom after declaring the class and it should be fine. python interpreter reads from top to bottom, you are calling the instance of class before defining the class, move line 3 and 4 to the bottom after declaring the class and it should be fine.

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

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