exceptions.AttributeError:class KeyAgent没有属性'delele_customer_node()'

时间:2014-11-18 18:31:48

标签: python

我有2个文件:customer.py& agent.py。它看起来像这样:

customer.py:

from agent import KeyAgent

class CustomerController(object):

    def __init__(self, container):                                          
        self.key =  container.agent
    def delete(self, path):
       KeyAgent.delele_customer_node()

agent.py:

class KeyAgent(service.Service):    

    def __init__(self):                              
        pass
    def delele_customer_node():
     ....

Python在运行时抛出此异常:

exceptions.AttributeError: class KeyAgent has no attribute 'delele_customer_node()'

即使我从agent.py导入了KeyAgent类,为什么无法从customer.py的delete()访问方法delele_customer_node()

1 个答案:

答案 0 :(得分:1)

您必须拼错方法名称(删除 l e?或删除 t e?)。 KeyAgent类确实有一个方法delete_customer_node(我认为这是一个错字)。

>>> class KeyAgent(object):
...   def delete_customer():
...     pass
... 
>>> KeyAgent.delete_customer
<unbound method KeyAgent.delete_customer>

这意味着,方法就在那里。但是你的代码很破碎。除非您使用staticmethodclassmethod装饰器,否则方法&#34;的第一个参数必须是&#34; self,您需要实例化该类才能调用它。如果您尝试直接调用此方法,请查看会发生什么:

>>> KeyAgent.delete_customer()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method delete_customer() must be called with KeyAgent instance as first argument (got nothing instead)