Skip to main content
 首页 » 编程设计

python之为什么 __getattribute__ 失败并返回 : TypeError: 'NoneType' object is not callable

2025年02月15日13jyk

这是我在这里的第一个问题,也是我在 Python 中的第一个项目。

我正在尝试存储名为 Ip500Device 的类的实例:

class Ip500Device(object): 
 
    list = [] 
    def __init__(self, shortMac, mac, status, deviceType): 
        self.__shortMac =shortMac 
        self.__mac=mac 
        self.__status=status 
        self.__deviceType=deviceType 
        self.__nbOfObjects=0 
        Ip500Device.list.append(self)     
 
    def __getattribute__(self, att): 
        if att=='hello': 
            return 0 

第一个测试只是一个“你好”,但之后我想获得所有属性。

在另一个类中,我正在创 build 备对象并将它们添加到列表中:

self.__ip500DevicesLst.append(Ip500Device.Ip500Device(lst[0],lst[1],lst[2],lst[3])) 
for abcd in self.__ip500DevicesLst: 
       print abcd.__getattribute__('hello') 

但是当我尝试打印时,程序返回此消息:

TypeError: 'NoneType' object is not callable 

我不太了解如何在 Python 中存储类实例。

请您参考如下方法:

print abcd.__getattribute__('hello') 

abcd.__getattribute__ 不是 __getattribute__ 方法。当您尝试计算 abcd.__getattribute__ 时,您实际上是在调用

type(abcd).__getattribute__(abcd, '__getattribute__') 

它返回 None,然后您尝试调用它,就好像它是一个方法一样。