【问题标题】:using an object passed as a function argument (function is defined inside another class)使用作为函数参数传递的对象(函数在另一个类中定义)
【发布时间】: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(调用AlertPublishInterfacepublish_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


【解决方案1】:

您收到此错误的原因是因为第一个参数是类本身。通常,这称为 self。

我也可以将其识别为 django(在这种情况下,您还应该继承,如果不是从其他 django 类,然后从 object 使其成为新样式类)

无论如何,只需将 self 作为您的第一个参数添加到 publish_alert 中,它可能会停止抛出该错误。

【讨论】:

  • @user1272435 如果对您有用,您应该接受堆栈上的答案。这是很好的教育(它可以提高你的接受分数)
【解决方案2】:

def publish_alert(o_alert_vo, dummy_remove): 应该是def publish_alert(self, o_alert_vo, dummy_remove):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 2017-08-07
    • 2015-05-29
    相关资源
    最近更新 更多