I'm looking for a elegant way to initialize many objects.
Let's say that I have a
from Utils.Config import Config, Properties
from Utils.Utils import PrettyPrint, SVN, Make, Bash, Git
self.config = Config()
self.make = Make()
self.env = Properties()
self.svn = SVN()
self.git = Git()
self.bash = Bash()
self.pretty_print = PrettyPrint()
I wouldn't create a class and place it as a base, that seems like overkill, too bulky of an approach.
Instead, you could create an auxiliary, helper, function that takes
setattr on it. An example of this with a couple of objects from
collections would look like this:
from collections import UserString, deque, UserDict def construct(instance, classes = (UserString, deque, UserDict)): for c in classes: setattr(instance, c.__name__.lower(), c())
Your case also fits nicely since your objects don't have some initial value required, so you can generalize and treat them in the same way :-).
Now your sample class can just call
construct() with the defaults in
__init__ and be done with:
class Foo: def __init__(self): construct(self)
construct function could of course be defined in
Utils as required or, as a method in the class.