I would like to be able to assign values to class' attributes without specifying them.
What i would like to make work
A)
class HoldingStuff(object):
def __init__(self, **args):
self.a = 1
self.b = '1'
self.c = 2
self.d = '3'
self.e = 4
for arg in args:
self[arg] = args[arg]
first = HoldingStuff(a=5)
second = HoldingStuff(d=2, e='2')
Of course i could just use a dictionary within the class which holds the values. There is a big downside to that however, because i can not use code completion for the attributes then. Which makes them hard to reuse and find. (The attributes are an extensive list of mutable parameters for a simulation.)
What does work (and is unpractical)
B)
class HoldingStuff(object):
def __init__(self, **args):
self['a'] = 1
self['b'] = '1'
self['c'] = 2
self['d'] = '3'
self['e'] = 4
for arg in args:
self[arg] = args[arg]
What kinda works
C)
class HoldingStuff(MutableMapping):
def __init__(self, **args):
self.a = 1
self.b = '1'
self.c = 2
self.d = '3'
self.e = 4
for arg in args:
self[arg] = args[arg]
def __setitem__(self, k, v) -> None:
if k =='a':
self.a = v
elif k =='b':
self.b = v
elif k =='c':
self.c = v
elif k =='d':
self.d = v
elif k =='e':
self.e = v
else:
print(k, "Not available for class.")
def __delitem__(self, v) -> None:
pass
def __getitem__(self, k) -> _VT_co:
if k =='a':
return self.a
elif k =='b':
return self.b
elif k =='c':
return self.c
elif k =='d':
return self.d
elif k =='e':
return self.e
else:
print(k, "Not available for class.")
def __len__(self) -> int:
return 5
def __iter__(self):
print("Not implemented")
The functions __setitem__ and __getitem__ seem obvious and repetitive!
Question Is there a way to reduce the source code of example C) to work in an implicit way of example A) ?
**argsat all? This issue never would have come up if you just wrote out your argument list normally and assigned attributes from the argument values normally.__getitem__and__setitem__methods (and arbitrary user-provided names wouldn't work with the code completion you're after either). Allowing callers to specify arbitrary keywords is the entire design purpose of**.