Skip to content

Instantly share code, notes, and snippets.

@f0t0n
Created October 31, 2013 18:13
Show Gist options
  • Save f0t0n/7254302 to your computer and use it in GitHub Desktop.
Save f0t0n/7254302 to your computer and use it in GitHub Desktop.
from collections import defaultdict
class Employee(object):
def __init__(self, first_name, last_name, speciality):
self._first_name = first_name
self._last_name = last_name
self._speciality = speciality
class Driver(Employee):
pass
# Just defaultdict(Employee) will try to return us an instance of class
# that will cause TypeError: __init__() takes exactly 4 arguments (1 given)
# So we're just returning the class itself.
factory = defaultdict(lambda: Employee)
for key in ('x', 'y', 'z'):
print type(factory[key]('Jimmy', 'Winter', 'engineer'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment