我正试图编写一个函数,其目的是要通过对象的__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