#--------------------------------------------------------------------------------------------------------------------------- class signal: lst = [] booklist = None data = [[0, 'nickname' , "nickname"], [1, 'name' , "notempty"]] def __init__(self, row, nrow): for key in self.data: if check(row[key[0]], key[2], "Лист: " + self.booklist + " Строка: " + str(nrow)): setattr(self, key[1], row[key[0]]) @staticmethod def exist(nickname): result = False for i in lst: result = result or i.nickname == nickname return result #--------------------------------------------------------------------------------------------------------------------------- class di(signal): booklist = "DI" data = [[0, 'nickname' , "nickname"], [1, 'in' , "in" ], [2, 'category' , "int" ], [3, 'log' , "bool" ], [4, 'name' , "notempty"], [5, 'inversion', "bool" ], [6, 'ton' , "time" ], [7, 'tof' , "time" ], [8, 'module' , "notempty"], [9, 'cleath' , "notempty"], [10, 'device' , "notempty"]] 

In the body of the program do:

 print(di.exist("P_cool_pg1_high")) 

I get the error: Traceback (most recent call last): File "C: \ Users \ and-work \ Desktop \ Bear SP \ python \ autogen_owen_1.py", line 197, in print (di.exist ("P_cool_pg1_high"))) File "C: \ Users \ and-work \ Desktop \ Bear SP \ python \ autogen_owen_1.py", line 117, in lst: NameError: name 'lst' is not defined

How to turn to lst? lst is by the way a list of signal class instances.

    3 answers 3

    Use not @classmethod, but @staticmethod. The decorator @classmethod substitutes the class of the method being called with the first parameter of the method.

    • I changed it to @staticmethod, changed the method a bit and got another error. - AND

    It is necessary to use @classmethod all the same, just specify the class cls when declaring the method and then cls.lst

    • Do you use any IDE? Just when I inserted your code, the IDE immediately highlighted me lst in the for i in lst: line for i in lst: they say it is unknown where this variable comes from. I am using PyCharm - gil9red

    Use the static method, but specify the type in it from which you will get the values:

     @staticmethod def exist(nickname): result = False for i in Signal.lst: 

    If it is important to you what type of @classmethod is called, then use the decorator @classmethod . For example, in signal and di may be different lst lists:

     @classmethod def exist(cls, nickname): result = False for i in cls.lst: 

    In python, use the first uppercase letter to name classes: Signal . There is a standard pep8 , which describes well how to make a python code better.