最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

class - (Python) cant access attribute of an object unless I check nullity, why? - Stack Overflow

programmeradmin9浏览0评论

A LeetCode problem which is about linked lists that goes with this structure:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

Gave an error while attempting to print the val of the next node, but still worked when given a nullity check (it never even went to the else statement).

Assuming l1 is an instance of the class ListNode with print(l1.nextNode) gives: ListNode{val: 4, next: ListNode{val: 3, next: None}}

And: nextNode = l1.next

Why does this Fail: print(nextNode.val)

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

While this Works:

if nextNode is not None:
    print(nextNode.val)
else:
    print("Node is None")

Extra: I wonder if the answer to the above is related to why this also Fails with try/catch:

try:
    print("try block executed")
    print(nextNode.val)
except:
    **print("except block executed1")
    print(nextNode.val)**
    if nextNode is not None:
        print("except block executed"2)
        print(nextNode.val)
    else:
        print("Node is None")

While this Works and prints try block executed:

try:
    print("try block executed")
    print(nextNode.val)
except:
    if nextNode is not None:
        print("except block executed")
        print(nextNode.val)
    else:
        print("Node is None")

EDIT: Found the cause, turned out that the code fails for certain test case where it has only 1 node, but when it succeeds, it shows another test case result

Found this out while trying to create a copy-able code, rookie mistake...

For more details check the LeetCode problem in the link provided at the start.

A LeetCode problem which is about linked lists that goes with this structure:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

Gave an error while attempting to print the val of the next node, but still worked when given a nullity check (it never even went to the else statement).

Assuming l1 is an instance of the class ListNode with print(l1.nextNode) gives: ListNode{val: 4, next: ListNode{val: 3, next: None}}

And: nextNode = l1.next

Why does this Fail: print(nextNode.val)

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

While this Works:

if nextNode is not None:
    print(nextNode.val)
else:
    print("Node is None")

Extra: I wonder if the answer to the above is related to why this also Fails with try/catch:

try:
    print("try block executed")
    print(nextNode.val)
except:
    **print("except block executed1")
    print(nextNode.val)**
    if nextNode is not None:
        print("except block executed"2)
        print(nextNode.val)
    else:
        print("Node is None")

While this Works and prints try block executed:

try:
    print("try block executed")
    print(nextNode.val)
except:
    if nextNode is not None:
        print("except block executed")
        print(nextNode.val)
    else:
        print("Node is None")

EDIT: Found the cause, turned out that the code fails for certain test case where it has only 1 node, but when it succeeds, it shows another test case result

Found this out while trying to create a copy-able code, rookie mistake...

For more details check the LeetCode problem in the link provided at the start.

Share Improve this question edited yesterday Yanal Nasir asked yesterday Yanal NasirYanal Nasir 211 silver badge4 bronze badges New contributor Yanal Nasir is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1
  • 2 Please give a code example that actually reproduces the given error when run, no commented code, no unknown attributes like l1.nextNode. – Klaus D. Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 1

When you reach the end of the list, nextNode will be None. None doesn't have a val attribute, so nextNode.val raises an exception.

If you check whether it's None first, you don't execute that erroneous expression, so there's no error.

When you use try/except, it catches the exception. But then in the except: block you try to print the same thing. It raises the exception again, and this time there's no try/except to catch it, so the program stops with an error.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论