Skip to content

Instantly share code, notes, and snippets.

Created October 18, 2017 08:43
Show Gist options
  • Save anonymous/34efa8fac218118eac598eb6be074e33 to your computer and use it in GitHub Desktop.
Save anonymous/34efa8fac218118eac598eb6be074e33 to your computer and use it in GitHub Desktop.
class Fruit:
def __init__(self, colour):
self.colour = colour
def __hash__(self):
return hash((self.__class__.__name__, self.colour))
def __eq__(self, other):
return type(self) == type(other) and self.colour == other.colour
def __repr__(self):
return '{} ({})'.format(self.__class__.__name__, self.colour)
class Apple(Fruit):
pass
class Berry(Fruit):
pass
from collections import Counter
fruits = [
Apple('red'), Apple('green'), Berry('black'), Berry('red'),
Berry('black'), Apple('red'), Berry('red'), Apple('green'),
Berry('blue'), Apple('pink')
]
counts = Counter(fruits)
#Counter({Apple (green): 2,
# Apple (pink): 1,
# Apple (red): 2,
# Berry (black): 2,
# Berry (blue): 1,
# Berry (red): 2})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment