使用作为函数参数传递的对象(函数在另一个类中定义)

时间:2012-03-15 19:45:04

标签: python function object arguments

我正在尝试访问传递给我的函数的对象(在我的类中定义)。

  • 基本上我正在调用类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'   

经过大量搜索后,能否排除上述错误......有人可以帮忙......?

谢谢......!(有点紧急......!)

2 个答案:

答案 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):