我正在为Django项目开发一个基本的python类子类系统,但是我遇到了一个奇怪的问题。
首先,分类的定义:
文件classes.py
class BaseAd(object):
""" base class for all the ads, with common parameters """
def __init__(self, dom, web, loc, cc, a, c, date, desc, hl, **kwargs):
self.domain = self.doDomain(dom)
self.url = self.doUrl(web)
self.description = self.doDescription(desc, hl)
self.location = self.doLocation(a, c, loc)
self.date = self.doDate(date)文件jobs.py
class JobAd(BaseAd):
""" extends BaseAd with more parameters """
def __init__(self, domain, url, location, countrycode, area, city,
index_date, description,
contract_multivalue, salary_min, company, job_title, **kwargs):
self.contract_type = self.doContract(contract_multivalue)
self.salary = self.doSalary(salary_min)
self.company = self.doCompany(company)
self.title = self.doTitle(job_title)
""" Super constructor call """
super(JobAd, self).__init__(
domain,
url,
location,
countrycode,
area,
city,
index_date,
description,
**kwargs
)这两个类都有各自的方法(doDomain、doSalary等),这些方法现在无关紧要,因为它们只是返回作为输入的字符串(将来会更好地实现,现在只是不需要)。kwargs只是用来存储一些无用的,但仍然返回原始数据(否则我会得到一个错误)。
JobAd类用作python接口晒黑的构造函数参数。在定义了一个类并将它传递给方法之后,它将solr响应中定义的字段(这只是一个dict)转换到该类中。因此,JobAd的init中定义的params必须具有与solr模式中的定义相同的名称。
这是实际的构造函数调用:
/path/to/myapp/resultsets/views_json.py in job_search_json
#lines splitted for better reading
#res is a solr search object
items = res.paginate(start=start, rows=res_per_page)
.sort_by("-index_date")
.sort_by("-score")
.sort_by("-md5")
.sort_by("-posted_date")
.execute(constructor=JobAd)在堆栈跟踪中的下一步是:
/path/to/sunburnt-0.6-py2.7.egg/sunburnt/search.py in execute
return self.transform_result(result, constructor)
...
▼ Local vars
Variable Value
self sunburnt.search.SolrSearch object at 0x7f8136e78450
result sunburnt.schema.SolrResponse object at 0x7f8136e783d0
constructor class 'myapp.models.jobs.JobAd'最后
/path/to/sunburnt-0.6-py2.7.egg/sunburnt/search.py in transform_result
result.result.docs = [constructor(**d) for d in result.result.docs]在最后一个"local“选项卡中,有一个结果字典(只有结构,而不是包含值的完整的dict ):
self sunburnt.search.SolrSearch object at 0x7f8136e78450
d {'area':
'city':
'contract_multivalue':
'country':
'countrycode':
'currency':
'description':
'district':
'domain':
'fileName':
'index_date':
'job_experience':
'job_field_multivalue':
'job_position_multivalue':
'job_title':
'job_title_fac':
'latitude':
'location':
'longitude':
'md5':
'salary_max':
'salary_min':
'study':
'url':
'urlPage':
}
constructor class 'tothego_frontend.sito_maynard.models.jobs.JobAd'在django.log文件中,除了DogSlow陷阱之外,没有其他错误,只告诉被捕获的行。
这是我正在犯的错误:
TypeError at /jobs/us/search/
__init__() takes exactly 13 arguments (12 given)我期望的行为不是我实际体验的行为:不是让我的类调用它的父构造函数(10个参数),而是使用它自己的init (14个参数)。
我也尝试使用旧的python类定义:超类中没有"object“;在子类‘init中,父类被初始化为BaseAd.init(self,.);我还试图将超级方法作为子类init ( la java)中的第一个语句调用,但是似乎没有什么变化。
我在这里做错什么了?
编辑:我固定了第二行的长度,有点过了!
根据的要求从DJANGO的STACKTRACE中添加了信息
最新消息:我开始认为,太阳光并不支持类继承,即使文档中没有关于它的任何内容。
新编辑:经过今天的一些测试,我发现(到目前为止)
现在总是漏掉一个论点。也许是“自我”?我真的不知道该去哪里看了,错误和以前一样(相同的堆栈跟踪)只是错误的参数。
实际上发现了问题,在init参数中添加了一些默认值,帮助我发现了真正的错误:输入中缺少字段。抱歉,小伙子们耽误了你们的时间,再次感谢你们的咨询。
发布于 2012-06-08 16:14:23
我使用了您的代码(将do*方法从__init__s中移除),并将其转化为一个简单的示例,试图在您陈述问题时重新创建它。
class BaseAd(object):
""" base class for all the ads, with common parameters """
def __init__(self, dom, web, loc, cc, a, c, date, desc, hl, **kwargs):
self.domain = dom
self.url = web
self.description = desc
self.location = loc
self.date = date
class JobAd(BaseAd):
""" extends BaseAd with more parameters """
def __init__(self, domain, url, location, countrycode, area, city,
index_date, description, solr_highlights,
contract_type, salary, company, job_title, **kwargs):
self.contract_type = contract_type
self.salary = salary
self.company = company
self.title = job_title
""" Super constructor call """
super(JobAd, self).__init__(
domain,
url,
location,
countrycode,
area,
city,
index_date,
description,
solr_highlights,
**kwargs
)
j = JobAd(1,2,3,4,5,6,7,8,9,10,11,12,13,kwarg1="foo",kwarg2="bar")当运行python2.7.2时,它执行得很好,没有错误。我认为,错误中提到的__init__可能是JobAd的,而不是超级的,因为JobAd的init实际上有14个参数,这就是错误所抱怨的。我建议找一个地方,在调用JobAdd's __init__时,参数不多。
正如其他人所说的,发布完整的堆栈跟踪并展示如何使用JobAd对于确定根本原因是非常宝贵的。
https://stackoverflow.com/questions/10952104
复制相似问题