0
我有點麻煩檢查如果瓦爾(對象)是類型字典。如果它的類型的字典不,比它會拋出一個錯誤做瓦爾(對象):如何檢查變量(對象)是否是字典類型?
if type(vars(vars(self.j)['element'])) is dict: # exception gets thrown here if not a dictionary
'this is a dictionary'
你不能使用類型(增值經銷商(對象))如果瓦爾(對象)沒有字典。那麼你如何檢查vars(object)是否是一個冠詞?
編輯:在回答下面的答案:
print type(vars(self.j)['element'])
print isinstance(vars(vars(self.j)['element']), dict) # this is a dict
print type(vars(self.j)['beverage'])
print isinstance(vars(vars(self.j)['beverage']), dict) # exception thrown because its not a dictionary
輸出:
<class '__main__.Element2'>
True
<type 'str'>
print isinstance(vars(vars(self.j)['beverage']), dict)
TypeError: vars() argument must have __dict__ attribute
編輯:響應布魯諾,我想要做這樣的事情:
def are_elements_equal(self, first_element, second_element, msg=None):
for i in vars(first_element).keys():
if type(vars(vars(first_element)[i])) is dict:
self.are_elements_equal(vars(first_element)[i], vars(second_element)[i])
else:
self.assertEqual(vars(first_element)[i], vars(second_element)[i])
檢查編輯。我試圖回答你爲什麼要使用變量的問題(vars(obj)) – BUInvent
這裏是我的完整代碼,如果你想看看它:http://www.codesend.com/view/9515db7496c9b3d36fd49231a5684f08/ – BUInvent