Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Account(object):
- def __init__(self, name, balance):
- self.name = name
- self.balance = balance
- self.observers = set()
- def __del__(self):
- for ob in self.observers:
- ob.close()
- del self.observers
- def register(self, observer):
- self.observers.add(observer)
- def unregister(self, observer):
- self.observers.remove(observer)
- def notify(self):
- for ob in self.observers:
- ob.update()
- def withdraw(self, amt):
- self.balance -= amt
- self.notify()
- class AccountObserver(object):
- def __init__(self, theaccount):
- self.theaccount = theaccount
- self.theaccount.register(self)
- def __del__(self):
- self.theaccount.unregister(self)
- del self.theaccount
- def update(self):
- print("Balance is %0.2f" % self.theaccount.balance)
- def close(self):
- print("Account no longer in use")
- a = Account("Ketty", 200000)
- a_mama = AccountObserver(a)
- a_tata = AccountObserver(a)
- a.unregister(a_mama)
- a.withdraw(10)
- Balance is 199990.00
- Account no longer in use
- Exception ignored in: <bound method AccountObserver.__del__ of <__main__.AccountObserver object at 0x024BF9F0>>
- Traceback (most recent call last):
- File "F:ProjectsTestPsrcmain.py", line 28, in __del__
- File "F:ProjectsTestPsrcmain.py", line 13, in unregister
- AttributeError: 'Account' object has no attribute 'observers'
- Exception ignored in: <bound method AccountObserver.__del__ of <__main__.AccountObserver object at 0x024BFEB0>>
- Traceback (most recent call last):
- File "F:ProjectsTestPsrcmain.py", line 28, in __del__
- File "F:ProjectsTestPsrcmain.py", line 13, in unregister
- AttributeError: 'Account' object has no attribute 'observers'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement