My ultimate goal is to have a class that has a regular constructor, but also a second constructor that can be used to create partially instantiated versions of the class.
class Foo(object):
def __init__(self, foo, bar):
self.foo = foo
self.bar = bar
def create_partial(self, **kwargs):
self.foo = kwargs.get('foo', None)
self.bar = kwargs.get('bar', None)
I noticed that pickle achieves this somehow. You can write __getstate__ and __setstate__ functions that only deal with one of the parameters and it ignores the fact you haven't defined one of them. For example
class Foo(object):
def__init__(self, foo, bar):
self.foo = foo
self.bar = bar
def __getstate__(self):
return {'foo': self.foo}
def __setstate__(self, d):
self.foo = d['foo']
Then I can pickle and unpickle an instance to get a semi instantiated object
myfoo = pickle.loads(pickle.dumps(Foo(1,2)))
print(myfoo.foo) # 1
print(myfoo.bar) # throws AttributeError
type(myfoo) # __main__.Foo
So, my question is, how does it manage to get a blank instance of the class, half instantiate it and have it still report its type as Foo, further, how can I do the same without having to go through pickle?
P.S. I know this is an insane thing to want to do, but I want to know how