AttributeError:“事件”对象没有属性“ setEvent”

时间:2019-09-17 22:00:33

标签: python python-3.x

我是Python的新手,并且我不熟悉面向对象的编程。 这是我的main.py:

from event import *
#First Try:
Event().setEvent("error","This is an error")
#Second Try:
a=Event()
a.setEvent("error","This is an error")

这是我的event.py:

class Blablaclass:
    .........
    .........
    #Too many codes.

class Event:
    def __init__(self):
        self._eventList = {"error": [], "info": []}
        self._event=""
        self._eventType=""
    def setEvent(self,eventType,event):
        self._eventType=eventType
        self._event=event
        self._eventList[self._eventType].append(self._event)
    def getEvents(self,eventType):
    #This place doesn't have to be right for now. Because I don't even use it.
        self._eventType = eventType
        if self._eventType!="all":
            for iterEvent in self._eventList[eventType]:
                return iterEvent
        else:
            for iterEvent in list(self._eventList["error"]+self._eventList["info"]):
                return iterEvent

我总是收到此错误的原因是什么?

AttributeError: 'Event' object has no attribute 'setEvent'

1 个答案:

答案 0 :(得分:0)

我不是Python向导,但是您的问题引起了我的兴趣。如果没有人先给出直接答案,这似乎是我们应该可以破解的东西。

我已将所有内容组合到一个程序中。请尝试运行以下代码变体,看看是否抛出相同的错误。这不会告诉我们出了什么问题,但是可以消除一些可能性。如果未引发任何错误,那么我们可以返回到导入的类,但可以简化为生成错误的绝对最小代码,然后看看能告诉我们什么。但是首先,请确保它对您的特定python注入或计算机没有什么影响

class Event:
    def __init__(self):
        self._eventList = {"error": [], "info": []}
        self._event=""
        self._eventType=""
    def setEvent(self,eventType,event):
        self._eventType=eventType
        self._event=event
        self._eventList[self._eventType].append(self._event)
        print(" setEvent() call executed fine.")

#from event import *
#First Try:
Event().setEvent("error","This is an error")
#Second Try:
a=Event()
a.setEvent("error","This is an error")