I'm cleaning up a python object class, focusing mainly on how the object is created. The __init__
method creates a an empty dictionary that needs to be filled almost instantly. But this should NOT happen within the __init__
, as the method used will vary widely. Here's an example:
class Storage:
def __init__(self):
self.data = {}
def fill_1(self):
self.data['solo'] = 'all alone'
def fill_2(self, buddy, bff):
self.data['buddy'] = buddy
self.data['bff'] = bff
def fill_3(self, that_guy, house):
self.data[that_guy] = house
Normally, I can just call one after the other like so:
box = Storage.Storage()
box.fill_1()
However, this can be overwhelming when I create many of these objects sequentially. My goal is to use the __init__
method with one of the fill
methods on the same line. I've tried using the call below:
box = Storage.Storage().fill_1()
But this does not create the object and instead returns None
. So I have two questions:
Is my code returning a None object because the line is calling an instance method?
And how can I create the Storage
object and then call it's fill
method within the same line?
*args
and**kwargs
to your__init__
function and do the work in the constructor? – Jurisdiction__init__
would require about 5+ default parameters. – Zwickau