我正在使用来自两个不同网页的数据集,但对于同一个人 - 数据集是合法信息。一些数据在第一页上可用,因此我使用正确的信息初始化被告对象,并将我当前没有数据的属性设置为null
。这是班级:
class Defendant(object):
"""holds data for each individual defendant"""
def __init__(self,full_name,first_name,last_name,type_of_appeal,county,case_number,date_of_filing,
race,sex,dc_number,hair_color,eye_color,height,weight,birth_date,initial_receipt_date,current_facility,current_custody,current_release_date,link_to_page):
self.full_name = full_name
self.first_name = first_name
self.last_name = last_name
self.type_of_appeal = type_of_appeal
self.county = county
self.case_number = case_number
self.date_of_filing = date_of_filing
self.race = 'null'
self.sex = 'null'
self.dc_number = 'null'
self.hair_color = 'null'
self.eye_color = 'null'
self.height = 'null'
self.weight = 'null'
self.birth_date = 'null'
self.initial_receipt_date = 'null'
self.current_facility = 'null'
self.current_custody = 'null'
self.current_release_date = 'null'
self.link_to_page = link_to_page
当我将半填写的被告对象添加到被告列表中时,情况如下:
list_of_defendants.append(Defendant(name_final,'null','null',type_of_appeal_final,county_parsed_final,case_number,date_of_filing,'null','null','null','null','null','null','null','null','null','null','null','null',link_to_page))
然后,当我从其他页面获取其余数据时,我将这些属性设置为 null,如下所示:
for defendant in list_of_defendants:
defendant.sex = location_of_sex_on_page
defendant.first_name = location_of_first_name_on_page
## Etc.
我的问题是:当我只有一半的信息要存储在类中时,是否有一种更Pythonic的方法来向类添加属性,或者一种不太难看的初始化类对象的方法?