【发布时间】:2016-07-28 21:48:54
【问题描述】:
我正在处理来自两个不同网页的数据集,但对于同一个人 - 数据集是有关的法律信息。第一页上提供了一些数据,因此我使用正确的信息初始化被告对象,并将我目前没有数据的属性设置为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.
我的问题是:当我只有一半想要存储的信息时,是否有更 Python 的方式来向类添加属性或初始化类对象的不那么丑陋的方式?
【问题讨论】:
-
您可以将参数默认为
'null',这样您就不需要在初始化时指定它们,您可以将最后一个指定为link_to_page = link_to_page并跳过其间的所有参数。 -
Null 值在 Python 中表示为
None,而不是字符串'null'。请不要对Mr. Null进行无端指责。
标签: python python-2.7 oop