Created
July 20, 2017 20:03
-
-
Save augustogoulart/1cb90ca768e6b14c93825e1269f27d55 to your computer and use it in GitHub Desktop.
Extending Python's dictionary
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
class LongNameDict(dict): | |
def longest_value(self): | |
longest = None | |
for key, value in self.items(): | |
if not longest or len(value) > len(longest): | |
longest = value | |
return longest | |
names = LongNameDict(first='josé', second='schwarzenegger') | |
assert names.longest_value() == 'schwarzenegger' | |
assert not names.longest_value() == 'josé' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment