我有一個類是誰_ 初始化 _函數需要相當多的關鍵字參數。我希望能夠基本上重寫這段代碼,使它在語法上更清晰(不太硬編碼)。最好我想能夠得到它,這樣簡單地增加一個關鍵字參數_ 初始化 _將分別更改所有屬性/參數的空函數。以Pythonic的方式處理大量的關鍵字參數
class Class :
def __init__ (self, kw0=0, kw1=1, kw2=2) :
''' The keyword arguments as strings. '''
self.Keys = ['kw0', 'kw1', 'kw2']
''' Their values. '''
self.Values = [kw0, kw1, kw2]
''' A dictionary made from the keys and values. '''
self.Dict = self.make_dict()
''' As individual attributes, '''
self.KW0, self.KW1, self.KW2 = self.Values
def make_dict (self) :
''' Makes a dictionary '''
keys = self.Keys
values = self.Values
_dict = {}
for i in xrange(len(keys)) :
key = keys[i]
value = values[i]
_dict[key] = value
return _dict
def null (self, kw0=None, kw1=None, kw2=None) :
''' The same keyword arguments as **__init__** but they all default
to **None**. '''
pass
c = Class()
print c.Keys
print c.Values
print c.Dict
print c.KW0
print c.KW1
print c.KW2
哇,這是一個很棒的代碼他們,謝謝! – rectangletangle