簡體   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