-1

I am using python 3.4

class baba():
  def __init__(self,a,b,c,d,e,f,g):
     self.a = a
     self.b = b
     self.c = c
     self.d = d
     self.e = e
     self.f = f
     self.g = g

IS there a shorter way to write this? Beside getting all of those as a dict

0

1 Answer 1

1

You can use **kwargs, then use setattr to create your instance attributes:

class baba():
    def __init__(self, **kwargs):
        for i in kwargs:
            setattr(self, i, kwargs[i])

b = baba(a=1,b=2,c=4,d=5)
print(b.a) # prints 1

Not the answer you're looking for? Browse other questions tagged or ask your own question.