i've encountered an issue while trying to implement the Observable Pattern in python. He is my code :
class Observer:
"""
Classe destinée à définir les modalités d'observation des évènements
"""
_observers = []
def __init__(self):
self._observers.append(self)
self._observables = {}
def observe(self, event_name, callback):
self._observables[event_name] = callback
class Event():
"""
Liste des évènements observables
"""
def __init__(self, name, *args, autofire = True):
self.name = name
# self.data = [arg1, arg2]
self.arg = [a for a in args]
# self.arg = args
# self.arg2 = arg2
if autofire:
self.fire()
def fire(self):
print(self.arg)
for observer in Observer._observers:
if self.name in observer._observables:
observer._observables[self.name](self.arg)
class Room(Observer):
""" Test de l'observation d'un évènement"""
def __init__(self):
Observer.__init__(self) # Observer's init needs to be called
print("La chambre est prête.")
def someone_arrived(self, who, place):
print(who, "est arrivé à", place, "!")
def main():
room = Room()
room.observe("someone arrived", room.someone_arrived)
Event("someone arrived", "Tof", "Toulouse")
if __name__ == "__main__":
main()
The issue lies in the __init__
function from the Event()
class. When i try to pass multiple arguments with *args
, it always get me back issues, whereas when i only put args
all works fine.
If someone could try it and give me back something to look at or explain me why it bugs, it'd be appreciated !
Thanks !
Aucun commentaire:
Enregistrer un commentaire