dangerChihuahua007
dangerChihuahua007

Reputation: 20875

Can one make Python constructors more concise?

My python code has a class from which instantiates objects representing countries. The class has a constructor.

class Country:
    def __init__(self, population, literacy, firms, area, populationDensity):
        self.population = population
        self.literacy = literacy
        self.firms = firms
        self.area = area
        self.populationDensity = populationDensity

Is there a way to make this code more concise? Here is the pseudocode for what I am seeking.

class Country:
    def __init__(self, population, literacy, firms, area, populationDensity):
        # assign object these properties in one line

Thank you.

Upvotes: 3

Views: 196

Answers (1)

jordanm
jordanm

Reputation: 34914

You can do this in one line, but it will only make the code more difficult to read and follow. Here is how you would do it.

class Country:
    def __init__(self, population, literacy, firms, area, populationDensity):
        (self.population, self.literacy, self.firms, self.area, self.populationDensity) = (population, literacy, firms, area, populationDensity)

Upvotes: 3

Related Questions