I'd like to make a dictionary subclass that takes in positional keywords in addition to the standard *args, **kwargs. I found this example on stackoverflow:
class attrdict(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.__dict__ = self
a = attrdict(x=1, y=2)
print a.x, a.y
print a['x']
b.x, b.y = 1, 2
print b.x, b.y
I'd like to know how to modfiy it to take in a positional argument. Something like:
class attrdict(dict):
def __init__(self,positional, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.positional=positional
### This doesn't
a = attrdict(50, 30, 20)
>>>TypeError: dict expected at most 1 arguments, got 2
This doesn't seem to want to accept multiple args. I was able to rectify this by overwriting the update method, something like:
def update(self, *args, **kwargs):
'do stuff'
However, it seems like whenever I define the update module, I lose the ability to define the instance variable, self.positional.
For example, if I do:
### Without overwriting update()
a
>>>{}
a.positional
>>>50
### With overwriting update()
a
>>> #No dictionary!
a.positional
>>> 50
Has anyone ever succesffuly made a custom dict that takes *args, and a positional argument?