Python 使用作为函数参数传递的对象(函数在另一个类中定义)
我试图访问传递给函数(在类中定义)的对象Python 使用作为函数参数传递的对象(函数在另一个类中定义),python,function,object,arguments,Python,Function,Object,Arguments,我试图访问传递给函数(在类中定义)的对象 本质上,我是在调用类AlertPublishInterface中定义的函数publish\u alert 调用方将名为AlertVO 一旦我通过publish\u alert接收到这个传递的参数实例,我只需尝试访问类AlertPublishInterface中传递的参数实例的数据成员(在该类中定义了调用的函数publish\u alert) 我在步骤2中获得了AttributeError,即当访问传递的参数实例的成员时: AttributeError:
- 本质上,我是在调用类
中定义的函数AlertPublishInterface
publish\u alert
- 调用方将名为
AlertVO
- 一旦我通过
接收到这个传递的参数实例,我只需尝试访问类publish\u alert
中传递的参数实例的数据成员(在该类中定义了调用的函数AlertPublishInterface
)publish\u alert
- 我在步骤2中获得了
,即当访问传递的参数实例的成员时: AttributeError:AlertPublishInterface实例没有属性“alert\u name”AttributeError
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\u alert
函数):
但是,它在上面带有类型错误的标记为#----1----和#----2----的行失败,似乎它正在将AlertVO
对象(o\u alert\u vo
实例)与AlertPublishInterface
类关联:
运行时屏幕输出的完整块:
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'
python test_publisher.py
测试中的发布者
批处理SLA
B
一些注释
实际值=2,预期值=1
回溯(最近一次呼叫最后一次):
文件“test_publisher.py”,第17行,在
警报\发布\ i.发布\警报(o \警报\ vo.警报\名称)
文件“/home/achmon/data\u process/AlertPublishInterface.py”,第26行,发布警报
打印o_alert_vo.alert_名称
AttributeError:AlertPublishInterface实例没有属性“alert\u name”
经过大量的搜索后,无法消除上述错误…有人能帮忙吗
谢谢…!(也有点紧急…!)出现此错误的原因是因为第一个参数是类本身。通常,这称为self 我还可以将其标识为django(在这种情况下,您还应该继承,如果不是从其他django类继承,那么应该从object继承,使其成为一个新的样式类)
无论如何,只要在publish\uAlert中添加self作为第一个参数,它可能会停止抛出该错误。出现此错误的原因是因为第一个参数是类本身。通常,这称为self 我还可以将其标识为django(在这种情况下,您还应该继承,如果不是从其他django类继承,那么应该从object继承,使其成为一个新的样式类)
无论如何,只要在发布警报中添加self作为第一个参数,它可能会停止抛出该错误。
def publish\u警报(o\u alert\vo,dummy\u remove):
应该是def publish\u警报(self,o\u alert\vo,dummy\u remove):
def publish\u警报(o\u alert\vo,dummy\u remove):
应该是def publish\u alert(self、o\u alert\u vo、dummy\u remove):
是AlertVO和AlertPublishInterface文件名以及类名吗?您应该遵循PEP8指南。文件名应该都是小写。类名是CamelCase,正如您所做的那样。AlertVO和AlertPublishInterface文件名以及类名吗?您应该遵循PEP8指南。文件名应该都是小写。Class name是CamelCase,正如您所做的那样。@user1272435如果某件事对您有效,您应该在堆栈上接受答案。这是一个好的指令(它可以提高您的接受分数)@user1272435如果某件事对您有效,您应该在堆栈上接受答案。这是一个好的指令(它可以提高您的接受分数)
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'