Issue
I want to check if the games have numbers in a particular key?
But i cant call the the class method check_1(), can you help me out?
It gives me ‘check_class’ object has no attribute ‘check_1’.
And do you have a idea how i can avoid the global variabel and the global fuction for the recursion, because its bad practice?
I am thankful for every suggestion to optimize the code, i am relatively new to programing.
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])
I’m getting an AttributeError
: ‘check_class’ object has no attribute ‘check_1’
Solution
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')
Demo:
>>> '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
As to why you put global check_1
there, I have no idea.
Answered By – timgeb
Answer Checked By – Terry (BugsFixing Volunteer)