我正在尝试访问传递给我的函数的对象(在我的类中定义)。
publish_alert
中定义的函数AlertPublishInterface
。publish_alert
一个名为AlertVO
publish_alert
收到这个传递的参数实例,我只是试图访问类AlertPublishInterface
中传递的参数实例的数据成员(其中定义了函数publish_alert
我在步骤2中得到AttributeError
,即在访问传递的参数实例的成员时:
AttributeError:AlertPublishInterface实例没有属性' alert_name'
以下是代码段:
AlertPublishInterface文件:
import datetime
import logging.config
import django_model_importer
logging.config.fileConfig('logging.conf')
logger = logging.getLogger('alert_publish_interface')
from alert.models import AlertRule #Database table objects defined in the model file
from alert.models import AlertType #Database table objects defined in the model file
import AlertVO #This is instance whose members am trying to simple access below...!
class AlertPublishInterface:
def publish_alert(o_alert_vo, dummy_remove):
print o_alert_vo.alert_name #-----1----#
alerttype_id = AlertType.objects.filter(o_alert_vo.alert_name,
o_alert_vo.alert_category, active_exact=1) #-----2----#
return
AlertVO定义为:
class AlertVO:
def __init__(self, alert_name, alert_category, notes,
monitor_item_id, monitor_item_type, payload):
self.alert_name = alert_name
self.alert_category = alert_category
self.notes = notes
self.monitor_item_id = monitor_item_id
self.monitor_item_type = monitor_item_type
self.payload = payload
调用代码段(调用AlertPublishInterface
' publish_alert
函数):
from AlertVO import AlertVO
from AlertPublishInterface import AlertPublishInterface;
o_alert_vo = AlertVO(alert_name='BATCH_SLA', alert_category='B',
notes="some notes", monitor_item_id=2, monitor_item_type='B',
payload='actual=2, expected=1')
print o_alert_vo.alert_name
print o_alert_vo.alert_category
print o_alert_vo.notes
print o_alert_vo.payload
alert_publish_i = AlertPublishInterface()
alert_publish_i.publish_alert(o_alert_vo)
然而,在标有#----- 1 ----#和#----- 2 ---#above以及类型错误的行上,它似乎无法关联{{1}带有AlertVO
类的对象(o_alert_vo
实例):
运行时完成屏幕输出块:
python test_publisher.py In test_publisher BATCH_SLA B some notes actual=2, expected=1 Traceback (most recent call last): File "test_publisher.py", line 17, in alert_publish_i.publish_alert(o_alert_vo.alert_name) File "/home/achmon/data_process/AlertPublishInterface.py", line 26, in publish_alert print o_alert_vo.alert_name AttributeError: AlertPublishInterface instance has no attribute 'alert_name'
经过大量搜索后,能否排除上述错误......有人可以帮忙......?
谢谢......!(有点紧急......!)
答案 0 :(得分:2)
您收到此错误的原因是因为第一个参数是类本身。通常,这称为自我。
我也可以将它识别为django(在这种情况下,你也应该继承,如果不是从其他django类继承,那么从object继承,使它成为一个新的样式类)
无论如何,只需将self添加为publish_ alert中的第一个参数,它就可能会停止抛出该错误。
答案 1 :(得分:1)
def publish_alert(o_alert_vo, dummy_remove):
应为def publish_alert(self, o_alert_vo, dummy_remove):