Skip to content

Instantly share code, notes, and snippets.

@augustogoulart
Last active July 20, 2017 20:02
Show Gist options
  • Save augustogoulart/e175b92794bb113b453d9f83f14e0773 to your computer and use it in GitHub Desktop.
Save augustogoulart/e175b92794bb113b453d9f83f14e0773 to your computer and use it in GitHub Desktop.
Extending python built-ins
"""
Simple example to explain how to extend built-in python objects
"""
class PowerList(list):
def search(self, name):
results = []
for obj in self:
if obj == name:
results.append(name)
return results
lista = PowerList()
lista.append('augusto')
lista.append('cintia')
lista.append('eduarda')
assert lista.search('augusto') == ['augusto']
assert lista.search('paulo') == []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment