Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setattr with kwargs, pythonic or not?

I'm using __init__() like this in some SQLAlchemy ORM classes that have many parameters (upto 20).

def __init__(self, **kwargs):
    for k, v in kwargs.iteritems():
        setattr(self, k, v)

Is it "pythonic" to set attributes like this?

like image 781
Imran Avatar asked Apr 11 '09 06:04

Imran


1 Answers

Yes. Another way to do this is.

def __init__(self, **kwargs):
    self.__dict__.update( kwargs )
like image 127
S.Lott Avatar answered Sep 20 '22 23:09

S.Lott