更加簡單,這是我的代碼:如何使我的代碼使用python
def set_floor_point(self,floor_point=None):
if self.data.get('stage'):
self.data['stage'] = {}
stage_number = self.get_stage_number()
floor_number = self.get_floor_number()
if self.data['stage'].get(stage_number):
self.data['stage'][stage_number] = {}
if self.data['stage'][stage_number].get('floor_point'):
self.data['stage'][stage_number]['floor_point'] = {}
if self.data['stage'][stage_number]['floor_point'].get(floor_number):
self.data['stage'][stage_number]['floor_point'][floor_number] = {}
self.data['stage'][stage_number]['floor_point'][floor_number] = floor_point
和字典我創建的時候第一次是這樣的:
stage =
{
0:{
'floor':{
0:{
'floor_point':0,
'gift':{}
}
}
}
}
,但我認爲我的代碼是不是非常好,它是太麻煩了,
等都是有人知道更簡單的方法,
感謝
默認情況下,當鍵不存在時,'dict'' get()'方法返回'None',所以也許你的意思是'如果不是some_dict.get():'。即使這可以被關鍵的*值*愚弄,它們在邏輯上是'False',比如'0',''''和'[]'。如果不是some_dict.has_key()或者如果key不在some_dict中,可能會更好。 – martineau 2011-04-02 11:39:50