chrise
chrise

Reputation: 4253

setting class attributes in class call

I am just wondering if there is a shortcut to setting default attributes. I generally would do

class class1(object):
    def __init__(self, att1='X'):
         self.att1 = att1

Is there a shortcut in the lines off

class class1(object):
    def __init__(self, self.att1='X'):

I assume at the moment of the call, the object does not exist, so seem logical that this way does not work, but maybe there is sort of less verbose way to deal with this when there is a lot more attributes to be set. Any ideas?

Upvotes: 0

Views: 53

Answers (1)

warvariuc
warvariuc

Reputation: 59584

Try something like:

class class1(object):
    att1='X'
    def __init__(self, **kwargs):
         self.__dict__.update(kwargs)

Upvotes: 1

Related Questions