I have:
class C:
aaa=2
class B:
def __init__ (self,name):
self.name
self.value
How can i define class C so when i dynamically set attribute to instance it make that attribute instance of class B. And attribute name of class B have to have attribute name equal string of name of that new attribute in class C and attribute value of B instance have to have value what set in new attribute in instance of class C.
Have to give me that result:
>> c=C()
>> c.whatever= 'strinstrinsstring'
>> isinstance(c.whatever,B)
True
>> c.whatever.value
'strinstrinsstring'
>>c.whatever.name
'whatever'
Just smartly override
__setattr__. If you want to do it only for a specific attribute, then put in a special case for the attribute name that you want to look for:If you want it for every attribute, just forget the
ifand it’ll do it for everything: