【发布时间】:2012-03-15 19:45:03
【问题描述】:
我正在尝试访问传递给我的函数(在我的类中定义)的对象。
- 本质上,我正在调用在类
AlertPublishInterface中定义的函数publish_alert。 - 调用者将一个名为
AlertVO的类的实例传递给publish_alert - 一旦我通过
publish_alert接收到这个传递的参数实例,我只是试图访问类AlertPublishInterface中传递的参数实例的数据成员(其中定义了调用函数publish_alert。 -
我在第 2 步中得到
AttributeError,即当访问传递的参数实例的成员时:AttributeError:AlertPublishInterface 实例没有属性“alert_name”
这里是代码sn-p:
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
调用代码sn-p(调用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---# 的行中出现类型错误,似乎它正在关联 AlertVO 对象(@987654335 @instance) 与AlertPublishInterface 类:
运行时屏幕输出的完整块:
python test_publisher.py 在 test_publisher BATCH_SLA 乙 一些笔记 实际=2,预期=1 回溯(最近一次通话最后): 文件“test_publisher.py”,第 17 行,在 alert_publish_i.publish_alert(o_alert_vo.alert_name) 文件“/home/achmon/data_process/AlertPublishInterface.py”,第 26 行,在 publish_alert 打印 o_alert_vo.alert_name AttributeError:AlertPublishInterface 实例没有属性“alert_name”经过大量搜索后无法摆脱上述错误...有人可以帮忙...吗?
谢谢...!(也有点急...!)
【问题讨论】:
-
AlertVO 和 AlertPublishInterface 文件名和类名一样吗?您应该遵循 PEP8 指南。文件名应该全部小写。正如你所做的那样,类名是 CamelCase。
标签: python function object arguments