Initializing many objects in an elegant way

Dzawor

I'm looking for a elegant way to initialize many objects.

Let's say that I have a Utils module, which has an interface to SVN, Git, Make and other stuff.

Currently, I'm doing it like this:

from Utils.Config import Config, Properties
from Utils.Utils import PrettyPrint, SVN, Make, Bash, Git

class Build(object):

    def __init__(self):
        self.config = Config()
        self.make = Make()
        self.env = Properties()
        self.svn = SVN()
        self.git = Git()
        self.bash = Bash()
        self.pretty_print = PrettyPrint()

and.. well, it doesn't looks good.

Is there any way to do this in more elegant way? I suppose that it can be a design problem, but I have no idea how to solve this.

I was thinking about creation Base class which will init all of these classes inside the Utils module. What do you think?

Dimitris Fasarakis Hilliard

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 self and 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)

the construct function could of course be defined in Utils as required or, as a method in the class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

elegant way to update many-to-many relationship

From Dev

Elegant way to update many properties with mongoose

From Dev

Elegant way of fetching multiple objects in custom order

From Dev

Is there an elegant way to have value objects in typescript?

From Mysql

Is there a more elegant way to query many to many tables on multiple ORs and ANDs?

From Dev

Elegant way of avoiding writing so many (click hide show)

From Dev

Elegant way to plot average loss of many trains in tensorflow

From Dev

Search for more elegant way to handle many nested try with resources

From Dev

What is the more elegant way to serialize my own objects with arrays in Swift

From Javascript

What is the most elegant way to insert objects between array elements?

From Dev

In JavaScript there an elegant way to merge two Objects and sum any common properties?

From Java

Elegant way of encapsulating behaviour that depends on the combination of involved objects?

From Dev

elegant way to extract a certain member from a list of objects

From Dev

Elegant way to select nested objects with the associated key based on a specific criteria

From Dev

The most elegant way to collect nonEmpty object fields from the list of objects

From Dev

Managing pointers to destroyed objects in a game engine in an elegant way

From Dev

Elegant way to convert json to list of objects in Python 3

From Dev

Elegant way to access objects created in qApplication main function

From Dev

Scala elegant way to convert a multiple optional objects to a different object if at least one of the objects defined

From Dev

Most efficient way to read many to many objects from GAE datastore

From Dev

Fast and elegant way to calculate fold change between several groups for many variables?

From Dev

Elegant way of counting how many times patterns from a file occur in another file

From Dev

Initializing objects dynamically

From Dev

Initializing nested Javascript objects

From Dev

Initializing objects of a model

From Dev

Initializing an Array to Store Objects

From Dev

Initializing multiple objects at once?

From Dev

initializing objects in delphi

From Dev

initializing in loop array with objects

Related Related

  1. 1

    elegant way to update many-to-many relationship

  2. 2

    Elegant way to update many properties with mongoose

  3. 3

    Elegant way of fetching multiple objects in custom order

  4. 4

    Is there an elegant way to have value objects in typescript?

  5. 5

    Is there a more elegant way to query many to many tables on multiple ORs and ANDs?

  6. 6

    Elegant way of avoiding writing so many (click hide show)

  7. 7

    Elegant way to plot average loss of many trains in tensorflow

  8. 8

    Search for more elegant way to handle many nested try with resources

  9. 9

    What is the more elegant way to serialize my own objects with arrays in Swift

  10. 10

    What is the most elegant way to insert objects between array elements?

  11. 11

    In JavaScript there an elegant way to merge two Objects and sum any common properties?

  12. 12

    Elegant way of encapsulating behaviour that depends on the combination of involved objects?

  13. 13

    elegant way to extract a certain member from a list of objects

  14. 14

    Elegant way to select nested objects with the associated key based on a specific criteria

  15. 15

    The most elegant way to collect nonEmpty object fields from the list of objects

  16. 16

    Managing pointers to destroyed objects in a game engine in an elegant way

  17. 17

    Elegant way to convert json to list of objects in Python 3

  18. 18

    Elegant way to access objects created in qApplication main function

  19. 19

    Scala elegant way to convert a multiple optional objects to a different object if at least one of the objects defined

  20. 20

    Most efficient way to read many to many objects from GAE datastore

  21. 21

    Fast and elegant way to calculate fold change between several groups for many variables?

  22. 22

    Elegant way of counting how many times patterns from a file occur in another file

  23. 23

    Initializing objects dynamically

  24. 24

    Initializing nested Javascript objects

  25. 25

    Initializing objects of a model

  26. 26

    Initializing an Array to Store Objects

  27. 27

    Initializing multiple objects at once?

  28. 28

    initializing objects in delphi

  29. 29

    initializing in loop array with objects

HotTag

Archive