Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将项目添加到列表中(如果是';这不是一个函数_Python_Python 3.x_Dictionary_Inspect - Fatal编程技术网

Python 将项目添加到列表中(如果是';这不是一个函数

Python 将项目添加到列表中(如果是';这不是一个函数,python,python-3.x,dictionary,inspect,Python,Python 3.x,Dictionary,Inspect,我现在正在尝试编写一个函数,它的目的是检查对象的\uu dict\uu,如果该项不是函数,则将其添加到字典中。 这是我的密码: def dict_into_list(self): result = {} for each_key,each_item in self.__dict__.items(): if inspect.isfunction(each_key): continue else: res

我现在正在尝试编写一个函数,它的目的是检查对象的
\uu dict\uu
,如果该项不是函数,则将其添加到字典中。 这是我的密码:

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.\uu dict\uuu)
,它应该返回
{u value:5}
您实际上是在检查
每个键是否是函数,很可能不是。实际上,您必须检查值,如下所示

if inspect.isfunction(each_item):
def dict_into_list(self):
    result = {}
    for each_key, each_item in self.__dict__.items():
        print(type(each_key), type(each_item))
        if inspect.isfunction(each_item) == False:
            result[each_key] = each_item
    return result
def dict_into_list(self):
    return {key: value for key, value in self.__dict__.items()
            if not inspect.isfunction(value)}
您可以通过如下方式包括
打印来确认这一点

if inspect.isfunction(each_item):
def dict_into_list(self):
    result = {}
    for each_key, each_item in self.__dict__.items():
        print(type(each_key), type(each_item))
        if inspect.isfunction(each_item) == False:
            result[each_key] = each_item
    return result
def dict_into_list(self):
    return {key: value for key, value in self.__dict__.items()
            if not inspect.isfunction(value)}
此外,您还可以使用字典理解编写代码,如下所示

if inspect.isfunction(each_item):
def dict_into_list(self):
    result = {}
    for each_key, each_item in self.__dict__.items():
        print(type(each_key), type(each_item))
        if inspect.isfunction(each_item) == False:
            result[each_key] = each_item
    return result
def dict_into_list(self):
    return {key: value for key, value in self.__dict__.items()
            if not inspect.isfunction(value)}

我可以想出一种通过python的dir和callable方法而不是
inspect
模块查找对象变量的简单方法

{var:self.var for var in dir(self) if not callable(getattr(self, var))}

请注意,这确实假设您没有重写类的
\uuuu getattr\uuuuu
方法来执行除获取属性以外的其他操作。

您可以展示一个自包含的示例来演示此问题吗?@Tyler function的意思是,您期望的是什么?在您的情况下,哪些不是功能?是的,您的功能是正确的。但是,您如何知道每个键很可能不是函数?@Tyler
\uuuuu dict\uuuu
将具有字符串键,并且值是相应的对象。