I'm finding myself writing a lot of classes with constructors like this:
class MyClass(object):
def __init__(self, foo, bar, foobar=1, anotherfoo=None):
self.foo = foo
self.bar = bar
self.foobar = foobar
self.anotherfoo = anotherfoo
Is this a bad code smell? Does Python offer a more elegant way of handling this?
My classes and even some of the constructors are more than just what I've shown, but I usually have a list of args passed to the constructor which just end up being assigned to similarly named members. I made some of the arguments optional to point out the problem with doing something like:
class MyClass(object):
def __init__(self, arg_dict):
self.__dict__ = arg_dict