我正試圖編寫一個函數,其目的是要通過對象的__dict__
並將項目添加到字典中,如果該項目不是函數。 這裏是我的代碼:將項目添加到列表如果它不是函數
def dict_into_list(self):
result = {}
for each_key,each_item in self.__dict__.items():
if inspect.isfunction(each_key):
continue
else:
result[each_key] = each_item
return result
如果我沒有記錯,inspect.isfunction應該認識到lambda表達式的功能,以及,是否正確?但是,如果我寫
c = some_object(3)
c.whatever = lambda x : x*3
那麼我的功能仍然包括lambda。有人可以解釋爲什麼這是嗎?
舉例來說,如果我有這樣一個類:
class WhateverObject:
def __init__(self,value):
self._value = value
def blahblah(self):
print('hello')
a = WhateverObject(5)
所以,如果我說print(a.__dict__)
,應該還給{_value:5}
你能展示一個自包含的例子來證明問題嗎? – BrenBarn 2015-02-24 06:17:50
@Tyler函數意味着,你期望什麼?你的情況不起作用? – Nilesh 2015-02-24 06:18:37