Pythonic way to initialize an object with a lot of parameters and default value

Peni

I need to create a class wich take a lot of parameters.

class Line:

    def __init__(self, name, nb = None, price_unit = None, total = None, 
                 unit = None, time = None, session = None ):

Every attribute will get the same name and the same value as the parameter passed to __init__().

So, of course i could do :

class MyClass:

    def __init__(self, name, nb = None, price_unit = None, total = None, 
                 unit = None, time = None, session = None ):
        self.name = name
        self.nb = nb
        self.price = price
        self.price_unit = price_unit
        self.total = total
        self.unit = unit
        self.time = time
        self.session = session

But that's a really heavy notation and doesn't seems pythonic to me. Do you know a more pythonic manner to do it ?

Moses Koledoye

Since you're setting defaults for all the keyword arguments to None, you can do the more readable:

class MyClass:

    def __init__(self, name, **kwargs):
        self.name = name
        for attr in ('nb', 'price_unit', 'total', 'unit', 'time', 'session'):
            setattr(self, attr, kwargs.get(attr))

However, I think your initial approach is pretty standard too, although you can improve it by removing the whitespaces between the arguments and their default values.

class MyClass:
    def __init__(self, name, nb=None, price_unit=None, total=None, 
                 unit=None, time=None, session=None):
        ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is the pythonic way to avoid default parameters that are empty lists?

From Javascript

javascript initialize each value in the object with a default value

From Dev

Pythonic way of checking parameter is an integer and setting a default value if no parameter

From Dev

pythonic way to combine a lot of startswith statements

From Dev

Clean pythonic composition with default parameters

From Dev

Pythonic way to determine if json object contains a certain value?

From Dev

What is the pythonic way to return value from an object, created by static method?

From Dev

Pythonic way to "default" a conditional statement?

From Dev

Pythonic way to default loop counters

From Dev

Is there a way to initialize a variable to a default value without knowing its type in C?

From Dev

What's the Pythonic way to initialize an array?

From Dev

initialize a default key value pair while returning RedisTemplate object in java

From Dev

Angular) Any better way to initialize the value of the Object in class when declaration?

From Dev

What is the pythonic way of adding a lot of setup code to an __init__ method?

From Dev

What is the Pythonic way to use a default value when function returns None or throws exception?

From Dev

pythonic way for function with loads of parameters and return values

From Dev

Pythonic way to intersect with unknown number of parameters

From Dev

Pythonic way to add optional parameters to API request

From Dev

Pythonic way to detect which optional parameters are present

From Dev

Pythonic way to overwrite a default argument with **kwargs?

From Dev

Passing constructor parameters to initialize an object

From Dev

Initialize object without pushing parameters

From Dev

Java - How to initialize parameters for an object?

From Dev

Initialize member reference with default value

From Dev

Initialize array with size and default value

From Dev

Initialize rest of array with a default value

From Java

What is the most pythonic way to check if an object is a number?

From Dev

Most pythonic way to add object to string?

From Dev

Pythonic way of try/except setting of object variables

Related Related

  1. 1

    What is the pythonic way to avoid default parameters that are empty lists?

  2. 2

    javascript initialize each value in the object with a default value

  3. 3

    Pythonic way of checking parameter is an integer and setting a default value if no parameter

  4. 4

    pythonic way to combine a lot of startswith statements

  5. 5

    Clean pythonic composition with default parameters

  6. 6

    Pythonic way to determine if json object contains a certain value?

  7. 7

    What is the pythonic way to return value from an object, created by static method?

  8. 8

    Pythonic way to "default" a conditional statement?

  9. 9

    Pythonic way to default loop counters

  10. 10

    Is there a way to initialize a variable to a default value without knowing its type in C?

  11. 11

    What's the Pythonic way to initialize an array?

  12. 12

    initialize a default key value pair while returning RedisTemplate object in java

  13. 13

    Angular) Any better way to initialize the value of the Object in class when declaration?

  14. 14

    What is the pythonic way of adding a lot of setup code to an __init__ method?

  15. 15

    What is the Pythonic way to use a default value when function returns None or throws exception?

  16. 16

    pythonic way for function with loads of parameters and return values

  17. 17

    Pythonic way to intersect with unknown number of parameters

  18. 18

    Pythonic way to add optional parameters to API request

  19. 19

    Pythonic way to detect which optional parameters are present

  20. 20

    Pythonic way to overwrite a default argument with **kwargs?

  21. 21

    Passing constructor parameters to initialize an object

  22. 22

    Initialize object without pushing parameters

  23. 23

    Java - How to initialize parameters for an object?

  24. 24

    Initialize member reference with default value

  25. 25

    Initialize array with size and default value

  26. 26

    Initialize rest of array with a default value

  27. 27

    What is the most pythonic way to check if an object is a number?

  28. 28

    Most pythonic way to add object to string?

  29. 29

    Pythonic way of try/except setting of object variables

HotTag

Archive