Sophie
Sophie

Reputation: 3

Method cannot access class variable of different class

I am writing an algorithm in Python that is supposed to sort children (out of a database table) into one of their chosen kindergarten wishes (also out of a database table) following certain criteria on who to guarantee a place in their chosen kindergarten first. For this I first wrote a KitaDAO class to link the programme to the database and fetch information out of certain tables, saving them as an object.

import pymysql
import json
from Kita import Kita
from Kind import Kind
from Element import Element

class KitaDAO():
    def __init__(self):
        self.db = pymysql.connect("localhost","projekt","projekt","kita" )
        self.cursor = self.db.cursor()
        self.kitaList = []
        self.kinderList = []


def getKitas(self):
    self.sql = "SELECT * FROM kitas"
    try:
        self.cursor.execute(self.sql)
        self.results = self.cursor.fetchall()
        for row in self.results:
            thisKita = Kita(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8])
            self.kitaList.append(thisKita)
    except Exception as e:
        print (e)
    return self.kitaList

def getWarteliste(self):
    self.sql = "SELECT * FROM warteliste"
    self.warteliste = []
    try:
        self.cursor.execute(self.sql)
        self.results = self.cursor.fetchall()
        for row in self.results:
            thisElement = Element(row[0],row[1],row[2],row[3],row[4],row[5],row[6])
            self.warteliste.append(thisElement)
    except Exception as e:
        print (e)
    return self.warteliste


def getKinder(self):
    self.sql = "SELECT * FROM kinderprofil"
    try:
        self.cursor.execute(self.sql)
        self.results = self.cursor.fetchall()
        for row in self.results:
            thisKind = Kind(row[0],row[1],row[2],row[3],row[4],row[5],row[6])
            self.kinderList.append(thisKind)
    except Exception as e:
        print (e)
    return self.kinderList

def getKindOnWarteliste(self,kita,wunschnummer):
    self.kinderList = []
    self.warteliste = []
    self.warteliste = self.getWarteliste()

    if (wunschnummer == 1):
        for i in self.warteliste:
            if (kita == i.getWunsch1()):
                self.kinderList.append(i.getKind())
    elif (wunschnummer == 2):
        for i in self.warteliste:
            if (kita == i.getWunsch2()):
                self.kinderList.append(i.getKind())
    elif (wunschnummer == 3):
        for i in self.warteliste:
            if (kita == i.getWunsch3()):
                self.kinderList.append(i.getKind())
    else:
        print("Error: Eine ungültige Wunschnummer wurde übergeben.")

    return self.kinderList

If needed I can also post the classes Element, Kind and Kita in here but they basically only contain an __init__ method and if needed a get method. They also work, I have tested that before.

My problem is now, that in my main class called Sortierung I made thisDAO an instance of KitaDAO and want to use it to call methods and such, as normally. Sadly the class variable thisDAO is not accessible in a method of Sortierung. So basically this code has the response:

File "Sortierung.py", line 3, in <module> class Sortierung():
File "Sortierung.py", line 30, in Sortierung checkBetreuung(i,warteliste)
File "Sortierung.py", line 11, in checkBetreuung KinderObjektListe = thisDAO.getKinder()
nameError: name 'thisDAO' is not defined

I marked the lines in the code under here.

from KitaDAO import KitaDAO

class Sortierung(): #---------- This is line 3
    kitas = []
    thisDAO = KitaDAO()
    kitas = thisDAO.getKitas()

def checkBetreuung(kita,kinderIDListe):
    KinderObjektListe = []
    KinderObjektListe = thisDAO.getKinder() #---------This is line 11

#left something out here that was irrelevant


for x in range(1,4):
    for i in kitas:
        warteliste = []
        warteliste = thisDAO.getKindOnWarteliste(i.getID,x)
        checkBetreuung(i,warteliste) #-------------This is line 30

Also BTW I am German that is why the variable names are all in German. Sorry :)

Upvotes: 0

Views: 56

Answers (1)

AKX
AKX

Reputation: 169407

You don't need the Sortierung class at all (this is not Java; not everything needs to be encapsulated in a class) – the root problem is thisDAO ends up being a class attribute of it.

Something like

from KitaDAO import KitaDAO

thisDAO = KitaDAO()
kitas = thisDAO.getKitas()

def checkBetreuung(kita, kinderIDListe):
    KinderObjektListe = thisDAO.getKinder()

for x in range(1,4):
    for i in kitas:
        warteliste = thisDAO.getKindOnWarteliste(i.getID(), x)
        checkBetreuung(i, warteliste)

should do the trick, barring any other problems.

Upvotes: 1

Related Questions