Skip to content

Instantly share code, notes, and snippets.

@sloria
Created June 30, 2013 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sloria/5895749 to your computer and use it in GitHub Desktop.
Save sloria/5895749 to your computer and use it in GitHub Desktop.
# Bad
# Can't change type of collection
# e.g. can't change employees from a list to a set
class Department:
def __init__(self, *employees):
self.employees = employees
for employee in department.employees:
...
# Better
class Department:
def __init__(self, *employees):
self._employees = employees
def __iter__(self):
return iter(self._employees)
for employee in department: # More readable, more composable
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment