繁体   English   中英

我不能在 Python 中调用 class 方法; AttributeError: 'check_class' object 没有属性 '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 没有属性'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' object 没有属性 'check_1'

Putting global check_1 before the definition of the function in the class body will create a function in the global scope, not a method.

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那里,我不知道。

是的,您的示例代码有几个问题。 您将不得不进一步研究 scope 的概念。 注意变量self.played_games_of_the_day的使用。 对构造函数的调用也没有设置dictionary_of_games - 它总是设置为空字典。

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

将来,您应该发布 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!')

暂无
暂无

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

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