繁体   English   中英

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

[英]AttributeError: 'NoneType' object has no attribute 'name'?

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

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

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

我想你误会了。

您的PeopleChain课程:

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

只是说,type(None) 等于 NoneType。 而不是使用

self.leader = None

用:

self.leader = []

试试下面的代码

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

您可以添加 try 和 catch,而不是添加 if 语句,这是一种更 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!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM