n1c*_*1c9 10 python oop python-2.7
我正在处理来自两个不同网页的数据集,但对于同一个人 - 数据集是合法信息.部分数据在第一页上可用,因此我使用正确的信息初始化Defendant对象,并设置我当前没有数据的属性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方法可以向类添加属性或者更简单的初始化类对象的方式?
首先,对设置为 null 的任何参数使用默认值。这样,您甚至不需要在实例化对象时指定这些参数(并且可以使用参数名称以任何顺序指定您确实需要的任何参数)。对于这些,您应该使用 Python 值None而不是字符串"null",除非有某些特定原因需要使用字符串。在 Python 2.x 中,具有默认值的参数需要放在最后,因此link_to_page需要移到这些参数之前。
然后,您可以通过更新实例的属性来设置属性__dict__,该属性存储附加到实例的属性。每个参数将被设置为具有相同名称的实例的属性。
def __init__(self, full_name, first_name, last_name, type_of_appeal, county, case_number, 
             date_of_filing, link_to_page, race=None, sex=None, dc_number=None,
             hair_color=None, eye_color=None, height=None, weight=None, birth_date=None,
             initial_receipt_date=None, current_facility=None, current_custody=None, 
             current_release_date=None):
      # set all arguments as attributes of this instance
      code     = self.__init__.__func__.func_code
      argnames = code.co_varnames[1:code.co_argcount]
      locs     = locals()
      self.__dict__.update((name, locs[name]) for name in argnames)
full_name您还可以考虑从其他两个名称参数合成。那么你就不必传入多余的信息并且它永远不会不匹配。您可以通过属性即时执行此操作:
@property
def full_name(self):
    return self.first_name + " " + self.last_name
为了更新,我添加了一个方法来执行此操作,但使用 接受仅关键字参数**。为了帮助保护数据的完整性,我们将仅更改已存在且设置为 的属性None。
def update(self, **kwargs):
    self.__dict__.update((k, kwargs[k]) for k in kwargs
                          if self.__dict__.get(k, False) is None)
然后您可以通过一次调用轻松更新所有您想要的:
defendant.update(eye_color="Brown", hair_color="Black", sex="Male")
为了确保实例已完全填写,您可以添加一个方法或属性来检查以确保所有属性都未填写None:
@property
def valid(self):
    return all(self.__dict__[k] is not None for k in self.__dict__)