Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. # avoid loops by coding instance attribute as-
  2. # signments as assignments to attribute dictionary keys
  3. class Accesscontrol:
  4. def __setattr__(self, attr, value):
  5. if attr == 'age':
  6. self.__dict__[attr] = value + 10 # Not self.name=val or setattr
  7. else:
  8. raise AttributeError(attr + ' not allowed')
  9.  
  10. #class pattern
  11. class Super:
  12. def method(self):
  13. print('in Super.method') # Default behavior
  14. def delegate(self):
  15. self.action() # Expected to be defined
  16. class Inheritor(Super): # Inherit method verbatim
  17. pass
  18. class Replacer(Super): # Replace method completely
  19. def method(self):
  20. print('in Replacer.method')
  21. class Extender(Super): # Extend method behavior
  22. def method(self):
  23. print('starting Extender.method')
  24. Super.method(self)
  25. print('ending Extender.method')
  26. class Provider(Super): # Fill in a required method
  27. def action(self):
  28. print('in Provider.action')
  29. if __name__ == '__main__':
  30. for klass in (Inheritor, Replacer, Extender):
  31. print('\n' + klass.__name__ + '...')
  32. klass().method()
  33. print('\nProvider...')
  34. x = Provider()
  35. x.delegate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement