Skip to content

Instantly share code, notes, and snippets.

@poros
Created October 5, 2015 22:58
Show Gist options
  • Save poros/d8bf6f59d140a52cbe4a to your computer and use it in GitHub Desktop.
Save poros/d8bf6f59d140a52cbe4a to your computer and use it in GitHub Desktop.
Use super() for dependency injection
from collection import Counter
from collection import OrderedDict
Counter('luppolo')
Counter({'p': 2, 'l': 2, 'o': 2, 'u': 1})
help(Counter)
| Method resolution order:
| Counter
| __builtin__.dict
| __builtin__.object
class OrderedCounter(Counter, OrderedDict):
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
OrderedCounter('luppolo')
OrderedCounter(OrderedDict([('l', 2), ('u', 1), ('p', 2), ('o', 2)]))
| Method resolution order:
| OrderedCounter
| collections.Counter
| collections.OrderedDict
| __builtin__.dict
| __builtin__.object
# USEFUL FOR MOCKING
class Robot(object):
def move(self, tool):
print "Moving %s" % tool
class CleaningBot(Robot):
def clean(self, tool):
super().move(tool)
class MockBot(Robot):
def move(self, tool):
print "Pretending to move %s" % tool
class MockedCleaningBot(CleaningBot, MockBot): pass
MockedCleaningBot().move('broom')
"Pretending to move broom"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment