mardi 6 novembre 2018

Pass class instances to other classes (or use global classes) in Python

i have some global classes, which means i use them on any other class.

Actual i do something like this:

class Log():
    def __init__(self):
        self.logs = []

    def log(self, msg):
        self.logs.append(msg)

class A():
    def __init__(self, logger):
        self.logger = logger

class B():
    def __init__(self, logger):
        self.logger = logger

class MyMain():
    def __init__(self):
        self.logger = Log()
        self.a = A(self.logger)
        self.b = B(self.logger)

I wonder if there is a more elegant way to use classes like that on all submodules.. i searched a lot for a solution of this problem, but i am confused about the answers. Is there a best practice on this..?

Thanks, Toni.

Aucun commentaire:

Enregistrer un commentaire