标签云

微信群

扫码加入我们

WeChat QR Code

I'm working with datasets from two different webpages, but for the same individual - the data sets are legal info on. Some of the data is available on the first page, so I initialize a Defendant object with the proper info, and set the attributes that I don't currently have the data for to null. This is the class:

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

And this is what it looks like when I add a half-filled out Defendant object to a list of defendants:

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))

then, when I get the rest of the data from the other page I update those attributes set to null like so:

        for defendant in list_of_defendants:
            defendant.sex = location_of_sex_on_page
            defendant.first_name = location_of_first_name_on_page
            ## Etc.

My question is: is there a more pythonic way to either add attributes to a class or a less ugly way of initializing the class object when I only have half of the information that I want to store in it?


You can have the parameters default to 'null' so that you don't need to specify them on initialization, you can specify the last one as link_to_page = link_to_page and skip all the ones in between.

2018年07月17日10分26秒

Null values are represented in Python as None, not as the string 'null'. Please don't make baseless accusations against Mr. Null.

2018年07月17日10分26秒

jwodder, noted!

2018年07月17日10分26秒