I am trying to set the attribute values of a certain class AuxiliaryClass than is instantiated in a method from MainClass class in the most efficient way possible.
AuxiliaryClass is instantiated within a method of MainClass - see below. However, AuxiliaryClass has many different attributes and I need to set the value of those attributes once the class has been instantiated - see the last 3 lines of my code.
Note: due to design constraints I cannot explain here, my classes only contain methods, meaning that I need to declare attributes as methods (see below).
class AuxiliaryClass(object):
def FirstMethod(self):
return None
...
def NthMethod(self):
return None
class MainClass(object):
def Auxiliary(self):
return AuxiliaryClass()
def main():
obj = MainClass()
obj.Auxiliary().FirstMethod = #some_value
...
obj.Auxiliary().NthMethod = #some_other_value
# ~~> further code
Basically I want to replace these last 3 lines of code with something neater, more elegant and more efficient. I know I could use a dictionary if I was instantiating AuxiliaryClass directly:
d = {'FirstMethod' : some_value,
...
'NthMethod' : some_other_value}
obj = AuxiliaryClass(**d)
But this does not seem to work for the structure of my problem. Finally, I need to set the values of AuxiliaryClass's attributes once MainClass has been instantiated (so I can't set the attribute's values within method Auxiliary).
Is there a better way to do this than obj.Auxiliary().IthMethod = some_value?
EDIT
A couple of people have said that the following lines:
obj.Auxiliary().FirstMethod = #some_value
...
obj.Auxiliary().NthMethod = #some_other_value
will have no effect because they will immediately get garbage collected. I do not really understand what this means, but if I execute the following lines (after the lines above):
print(obj.Auxiliary().FirstMethod())
...
print(obj.Auxiliary().NthMethod())
I am getting the values I entered previously.
MainClassis a class factory. Have you looked at meta classes. Search Google for examples. Here are a few from Jake VanderPlus, Eli Bendersky and Ionel. I'll try to make a quick example in minute.obj.Auxiliary().FirstMethod = #some_valuebasically do nothing at all, as soon as that line ends?obj.Auxiliary()returns a new AuxiliaryClass instance, which you modify an attribute of... And then that instance almost immediately gets garbage collected because its refcount drops to zero. Note that reassigning an instance's attribute will have no effect on any other instance of that class, and no effect on the class itself.propertydecorator for your methods so they appear as attributes, that way you won't need to use()to "call" them and (2) instead of using a meta class this might also be an example of inheritance, if the auxiliary class can inherit from the main class, this may solve your dilemna.AuxiliaryClassAuxiliaryClassis encapsulated within the instance ofMainClassso I can simply set the attributes of i as I wish. Anyway, when I run the code what you are describing does not seem to be happening.