Reputation: 68
I successfully implemented a Descriptor to clean up my code and it worked fine so far. At this moment my code looks basically like this
class ABC:
att1 = Descriptor(arg1)
att2 = Descriptor(arg2)
att3 = Descriptor(arg3)
However, to further reduce repititions, Id like to do something like this
class ABC:
att_arg = {'att1':arg1,
'att2':arg2,
'att3':arg3}
for att, arg in att_arg.items():
setattr(ABC, att, Descriptor(arg))
but this leads to a NameError: name 'ABC' is not defined
.
Already thought about a wrapper class to do this, but does anyone know an elegant solution to this?
Upvotes: 1
Views: 44
Reputation: 345
What about do it in the constructor? From there you can use self
object to set the attributes:
class ABC:
ATTR_ARG = {'att1':arg1,
'att2':arg2,
'att3':arg3}
def __init__(self):
for attr, arg in ATTR_ARG.items():
setattr(self, attr, Descriptor(arg))
Upvotes: 1