當試圖實例下面的類我收到以下錯誤:的Python:類定義與** kwargs
"TypeError: __init__() takes exactly 2 arguments (3 given)"
你知道會產生什麼問題?下面是類的定義:
class db_create_table():
'''
doc here
'''
def __init__(self,TableName, **kwargs):
self.TableName = TableName
for k,v in kwargs.iteritems():
setattr(self, k, k)
schema = {"id" : { "type":"Integer", "primary":"primary_key=True", "unique":"unique = True"},
"col1" : { "type":"String()", "primary":"primary_key=False", "unique":"unique = True"},
"col2" : { "type":"String()", "primary":"primary_key=False", "unique":"unique = False"},
"col3" : { "type":"String()", "primary":"primary_key=False", "unique":"unique = False"},
"col4" : { "type":"String()", "primary":"primary_key=False", "unique":"unique = False"},
"CreatedOn" : { "type":"DateTime", "primary":"", "unique":"unique = False"},
"UpdatedOn" : { "type":"DateTime", "primary":"primary_key=False", "unique":"unique = False"},
}
db_create_table('Table1', schema)
詳細說明此答案,您還可以編寫(簡化):'db_create_table('Table1',id = 1,col1 = 2,col2 = 3,CreatedOn ='2017-09')' – rocksteady