I cant call a class method in Python; AttributeError: 'check_class' object has no attribute 'check_1'

I cant call a class method in Python; AttributeError: 'check_class' object has no attribute 'check_1'

我想检查游戏是否有特定键的数字?

但是我无法调用 class 方法 check_1(),你能帮帮我吗? 它给了我 'check_class' object has no attribute 'check_1'.

你知道我如何避免递归的全局变量和全局函数,因为它是不好的做法吗?

我很感谢每一个优化代码的建议,我对编程还比较陌生。

from queue import Empty

class check_class :
    
    def __init__(self, dict1={}, dict2={}, dict3={}, dict4={}, dict5={}, dict6={}, dict7={}, dict8={}, dict9={}, dict10={}, dict11={}, dict12={}, dictionary_of_games ={}):
        self.dict1 = dict1
        self.dict2 = dict2
        self.dict3 = dict3
        self.dict4 = dict4
        self.dict5 = dict5
        self.dict6 = dict6
        self.dict7 = dict7
        self.dict8 = dict8
        self.dict9 = dict9
        self.dict10 = dict10
        self.dict11 = dict11
        self.dict12 = dict12
        self.dictionary_of_games = dictionary_of_games

        dictionary_of_games = [
        self.dict1,
        self.dict2,
        self.dict3,
        self.dict4,
        self.dict5,
        self.dict6,
        self.dict7,
        self.dict8,
        self.dict9,
        self.dict10,
        self.dict11,
        self.dict12
        ]

    global played_games_of_the_day
    played_games_of_the_day =[]

    global check_1
    def check_1(self, **kwargs):
        hht = kwargs.get("halbzeit_h_tore")
        hat = kwargs.get("halbzeit_a_tore")
        c = 1
        if hht == "-" and hat == "-":
            played_games_of_the_day.append(0)
            c += 1
            return check_1(self, self.dictionary_of_games[c])
        elif int(hht) == int and int(hat) == int :
            c += 1
            played_games_of_the_day.append(1)
            return check_1(self, self.dictionary_of_games[c])
        #if dict empty pass#
        elif Empty:
            pass
        
        for i in played_games_of_the_day:
            if all(x==0 for x in played_games_of_the_day):
                print("all zero")
            elif all(x==1 for x in played_games_of_the_day):
                print("all one")

a = {0: {'spieltag': '1. Spieltag:', 'tag': 'Fr', 'd_u': '13.08.2021 20:30', 'team1': 'Borussia M´gladbach', 'team2': 'Bayern München', 'h_tore': '1', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '}, 
     1: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'Arminia Bielefeld', 'team2': 'SC Freiburg', 'h_tore': '0', 'a_tore': '0', 'halbzeit_h_tore': '0', 'halbzeit_a_tore': '0', 'absage': ' '}, 
     2: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'VfL Wolfsburg', 'team2': 'VfL Bochum', 'h_tore': '1', 'a_tore': '0', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '0', 'absage': ' '}, 
     3: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'FC Augsburg', 'team2': 'TSG Hoffenheim', 'h_tore': '0', 'a_tore': '4', 'halbzeit_h_tore': '0', 'halbzeit_a_tore': '1', 'absage': ' '}, 
     4: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'VfB Stuttgart', 'team2': 'Greuther Fürth', 'h_tore': '5', 'a_tore': '1', 'halbzeit_h_tore': '2', 'halbzeit_a_tore': '0', 'absage': ' '}, 
     5: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': '1. FC Union Berlin', 'team2': 'Bayer Leverkusen', 'h_tore': '1', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '}, 
     6: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 18:30', 'team1': 'Borussia Dortmund', 'team2': 'Eintracht Frankfurt', 'h_tore': '5', 'a_tore': '2', 'halbzeit_h_tore': '3', 'halbzeit_a_tore': '1', 'absage': ' '}, 
     7: {'spieltag': '1. Spieltag:', 'tag': 'So', 'd_u': '15.08.2021 15:30', 'team1': 'FSV Mainz 05', 'team2': 'RB Leipzig', 'h_tore': '1', 'a_tore': '0', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '0', 'absage': ' '}, 
     8: {'spieltag': '1. Spieltag:', 'tag': 'So', 'd_u': '15.08.2021 17:30', 'team1': '1. FC Köln', 'team2': 'Hertha BSC Berlin', 'h_tore': '3', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '}}

b = check_class(a)
b.check_1(b.dictionary_of_games[0])

我得到一个 AttributeError: 'check_class' 对象没有属性 'check_1'

在 class 主体中的函数定义之前放置 global check_1 将在全局范围内创建函数,而不是方法。

class A:
    global check_1
    def check_1(self):
        print('check_1')
    def check_2(self):    
        print('check_2')

演示:

>>> 'check_1' in A.__dict__
False
>>> 'check_2' in A.__dict__
True
>>> check_1
<function __main__.check_1(self)>
>>> A().check_1()
[...]
AttributeError: 'A' object has no attribute 'check_1'
>>> A().check_2()
check_2

至于你为什么把global check_1放在那里,我不知道。

是的,您的示例代码有几个问题。 您将不得不多检查一下作用域的概念。 注意变量 self.played_games_of_the_day 的使用。 此外,对构造函数的调用未设置 dictionary_of_games - 它始终设置为空字典。

我已经更正了您的代码,理论上它可能 运行,但我不确定应该做什么。在这样做的过程中,我删除了字典 dict1、dict2 等,因为它们没有被使用。

将来您应该 post python 让您更准确地了解您的问题并将您的问题减少到最小示例的错误...

from queue import Empty

class Check_class :
    
    def __init__(self, dictionary_of_games):
        self.dictionary_of_games = dictionary_of_games
        self.played_games_of_the_day = []

    def check_1(self):
        for entry in self.dictionary_of_games.values():
            hht = entry.get("halbzeit_h_tore")
            hat = entry.get("halbzeit_a_tore")
            if hht == "-" and hat == "-":
                print(f"{hht} {hat}")
                self.played_games_of_the_day.append(0)
            elif hht.isnumeric() and hat.isnumeric():
                print(f"{hht}:{hat}")
                self.played_games_of_the_day.append(1)
            #if dict empty pass#
            elif Empty:
                pass
        
        if all(x==0 for x in self.played_games_of_the_day):
            print("all zero")
        elif all(x==1 for x in self.played_games_of_the_day):
            print("all one")

a = { 0: {'spieltag': '1. Spieltag:', 'tag': 'Fr', 'd_u': '13.08.2021 20:30', 'team1': 'Borussia M´gladbach', 'team2': 'Bayern München', 'h_tore': '1', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '}, 
    1: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'Arminia Bielefeld', 'team2': 'SC Freiburg', 'h_tore': '0', 'a_tore': '0', 'halbzeit_h_tore': '0', 'halbzeit_a_tore': '0', 'absage': ' '}, 
    2: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'VfL Wolfsburg', 'team2': 'VfL Bochum', 'h_tore': '1', 'a_tore': '0', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '0', 'absage': ' '},
    3: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'FC Augsburg', 'team2': 'TSG Hoffenheim', 'h_tore': '0', 'a_tore': '4', 'halbzeit_h_tore': '0', 'halbzeit_a_tore': '1', 'absage': ' '}, 
    4: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': 'VfB Stuttgart', 'team2': 'Greuther Fürth', 'h_tore': '5', 'a_tore': '1', 'halbzeit_h_tore': '2', 'halbzeit_a_tore': '0', 'absage': ' '}, 
    5: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 15:30', 'team1': '1. FC Union Berlin', 'team2': 'Bayer Leverkusen', 'h_tore': '1', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '}, 
    6: {'spieltag': '1. Spieltag:', 'tag': 'Sa', 'd_u': '14.08.2021 18:30', 'team1': 'Borussia Dortmund', 'team2': 'Eintracht Frankfurt', 'h_tore': '5', 'a_tore': '2', 'halbzeit_h_tore': '3', 'halbzeit_a_tore': '1', 'absage': ' '}, 
    7: {'spieltag': '1. Spieltag:', 'tag': 'So', 'd_u': '15.08.2021 15:30', 'team1': 'FSV Mainz 05', 'team2': 'RB Leipzig', 'h_tore': '1', 'a_tore': '0', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '0', 'absage': ' '}, 
    8: {'spieltag': '1. Spieltag:', 'tag': 'So', 'd_u': '15.08.2021 17:30', 'team1': '1. FC Köln', 'team2': 'Hertha BSC Berlin', 'h_tore': '3', 'a_tore': '1', 'halbzeit_h_tore': '1', 'halbzeit_a_tore': '1', 'absage': ' '}
    }
    
b = Check_class(a)
b.check_1()
print('Done!')