【问题标题】:AttributeError: NoneType object has no attributeAttributeError:NoneType 对象没有属性
【发布时间】:2015-05-31 06:56:50
【问题描述】:

我作为一个python新手,在执行作者收到的代码时收到以下错误消息:

line 412, in add_family
    mother_birth_ref = mother.get_birth_ref()
AttributeError: 'NoneType' object has no attribute 'get_birth_ref'

代码的相关部分必须是这里的这个:

def write_child_stats(self):
    file = open("%s/intern/child-stats.txt" % self.dir_name, 'w')

def add_family(family, marriage_event, divorce_event):
    father_handle = family.get_father_handle()
    mother_handle = family.get_mother_handle()

    father = self.database.get_person_from_handle(father_handle)
    father_birth_ref = father.get_birth_ref()
    father_birth = None
    if father_birth_ref:
        father_birth = self.database.get_event_from_handle(father_birth_ref.ref)

    mother = self.database.get_person_from_handle(mother_handle)
    mother_birth_ref = mother.get_birth_ref()
    mother_birth = None
    if mother_birth_ref:
        mother_birth = self.database.get_event_from_handle(mother_birth_ref.ref)

    children = []
    child_refs = family.get_child_ref_list()
    for child_handle in child_refs:
        child = self.database.get_person_from_handle(child_handle.ref)
        if child:
            child_birth_ref = child.get_birth_ref()
            if child_birth_ref:
                child_birth = self.database.get_event_from_handle(child_birth_ref.ref)
                children.append("%04d-%02d-%02d" % child_birth.get_date_object().get_ymd())

我该如何解决这个问题?

【问题讨论】:

  • get_person_from_handlemother_handle 返回None。发布它。

标签: python nonetype


【解决方案1】:
 mother = self.database.get_person_from_handle(mother_handle)

此行返回None。检查get_person_from_handle函数。

或者,您可以添加一个检查:

mother = self.database.get_person_from_handle(mother_handle)
mother_birth_ref = None
mother_birth = None
if mother:
    mother_birth_ref = mother.get_birth_ref()
if mother_birth_ref:
    mother_birth = self.database.get_event_from_handle(mother_birth_ref.ref)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多