Last active
July 20, 2017 20:02
-
-
Save augustogoulart/e175b92794bb113b453d9f83f14e0773 to your computer and use it in GitHub Desktop.
Extending python built-ins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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