Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # avoid loops by coding instance attribute as-
- # signments as assignments to attribute dictionary keys
- class Accesscontrol:
- def __setattr__(self, attr, value):
- if attr == 'age':
- self.__dict__[attr] = value + 10 # Not self.name=val or setattr
- else:
- raise AttributeError(attr + ' not allowed')
- #class pattern
- class Super:
- def method(self):
- print('in Super.method') # Default behavior
- def delegate(self):
- self.action() # Expected to be defined
- class Inheritor(Super): # Inherit method verbatim
- pass
- class Replacer(Super): # Replace method completely
- def method(self):
- print('in Replacer.method')
- class Extender(Super): # Extend method behavior
- def method(self):
- print('starting Extender.method')
- Super.method(self)
- print('ending Extender.method')
- class Provider(Super): # Fill in a required method
- def action(self):
- print('in Provider.action')
- if __name__ == '__main__':
- for klass in (Inheritor, Replacer, Extender):
- print('\n' + klass.__name__ + '...')
- klass().method()
- print('\nProvider...')
- x = Provider()
- x.delegate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement