Python:如何创建派生类的初始化程序

时间:2014-11-28 12:38:17

标签: python constructor

我有一个班级order_order

class order_order(osv.osv):

    _inherit = "sale.order"

    _columns = {
        'order_completed_date' : fields.char('Order Completed Date'),
        .
        .
        .
    }

根据this SO帖子,我写了以下初始化程序:

def __init__(self, **kwargs):
    super(order_order, self).__init__(**kwargs)
    self.order_completed_date = order_completed_date
    .
    .
    .

我正在尝试创建一个对象:

order_order.__new__(order_completed_date=order_completed_date, ...)

但是我收到以下错误:

TypeError: __new__() got an unexpected keyword argument 'order_completed_date'

编辑:使用以下内容获得相同的错误:

order_order(order_completed_date=order_completed_date, ...)

EDIT2: 完整错误:

Traceback (most recent call last):
  File "/home/nish/repos/stage/openerp/web/addons/web/http.py", line 292, in dispatch
    r = method(self, **self.params)
  File "/home/nish/repos/stage/openerp/web/addons/web/controllers/spree_api.py", line 118, in some_html
    order_order(order_completed_date=order_completed_date, order_id=order_id, product_id=product_id, product_name=product_name, size=size, product_cost_cp=product_cost_cp, product_cost_sp=product_cost_sp, product_cost_mrp=product_cost_mrp, product_creation_date=product_creation_date, product_taxon=product_taxon, user_email=user_email, user_name=user_name, user_address=user_address, user_city=user_city, city_zip_code=city_zip_code, user_state=user_state, user_country=user_country)
TypeError: __new__() got an unexpected keyword argument 'user_address'

我做错了什么? 我该如何纠正错误?

1 个答案:

答案 0 :(得分:2)

__new__是类型的构造函数,您通常不希望像那样使用它。创建对象的常规方法(包括调用其初始化器)是:

order_order(order_completed_date=order_completed_date, ...)

所以,只需调用类型;并且该调用的参数将被传递给初始化器。