AttributeError:'NoneType'对象没有属性'name'?

时间:2017-10-07 02:46:42

标签: python class oop

我有一个我无法解决的问题。这是我的代码:

class Person:
  def __init__(self, name):
    self.name = name
    self.next = None

class PeopleChain:
  def __init__(self, names):
    if names == []:
        self.leader = None
    else:
      self.leader = Person(names[0])
      current_person = self.leader
      for name in names[1:]:
        current_person.next = Person(name)
        current_person = current_person.next
  def get_nth(self, n):
    """Return the name of the n-th person in the chain.
    >>> chain = PeopleChain(['a', 'b', 'c'])
    >>> chain.get_nth(1)
    'a'
    """
    current_person = self.leader
    for i in range(1, n):
      if i < n:
        current_person = current_person.next
    return current_person.name

例如,当我使用chain.get_nth(4)时,它会显示:

  

AttributeError: 'NoneType' object has no attribute 'name'

在我更改之后,这是我的代码:

def get_nth(self, n):
    current_person = self.leader
    for i in range(1, n):
        if i < n:
            current_person = current_person.next
            if current_person is None:
                raise SomeError #user-defined error
    return current_person.name

但它仍然无效。为什么它不起作用,我该如何解决?非常感谢你。

2 个答案:

答案 0 :(得分:0)

我觉得你误解了。

您的 PeopleChain 类:

class PeopleChain:
def __init__(self, names):
    if names == []:

self.leader =无 ##!??

else:
        self.leader = Person(names[0])
        current_person = self.leader
        for name in names[1:]:
            current_person.next = Person(name)
            current_person = current_person.next
def get_nth(self, n):
"""Return the name of the n-th person in the chain.
>>> chain = PeopleChain(['a', 'b', 'c'])
>>> chain.get_nth(1)
'a'
"""
current_person = self.leader
for i in range(1, n):
    if i < n:
        current_person = current_person.next
return current_person.name

只是说,     型(无) 等于NoneType。而不是使用

self.leader = None

使用:

self.leader = []

答案 1 :(得分:0)

尝试以下代码

class ShortChainError(Exception):
    def __init__(self,*args,**kwargs):
        Exception.__init__(self,*args,**kwargs)

class Person:
  def __init__(self, name):
    self.name = name
    self.next = None

class PeopleChain:
  def __init__(self, names):
    if names == []:
        self.leader = None
    else:
      self.leader = Person(names[0])
      current_person = self.leader
      for name in names[1:]:
        current_person.next = Person(name)
        current_person = current_person.next
  def get_nth(self, n):
    """Return the name of the n-th person in the chain.
    >>> chain = PeopleChain(['a', 'b', 'c'])
    >>> chain.get_nth(1)
    'a'
    """
    current_person = self.leader
    for i in range(1, n):
      if i < n:
        try:
          current_person = current_person.next
          name = current_person.name
        except AttributeError:
          raise ShortChainError("Your Message Here!!!")
    return name

不是添加if语句,而是可以添加try并捕获它更加pythonic的方式。所以你的代码将成为

  if i < n:
    try:
      current_person = current_person.next
      name = current_person.name
    except AttributeError:
      raise ShortChainError("Your Message Here!!!")
return name

现在运行这样的代码

PeopleChain(['a', 'b', 'c']).get_nth(4)

它会抛出一个自定义错误异常,例如

    raise ShortChainError("Your Message Here!!!")
__main__.ShortChainError: Your Message Here!!!