admin管理员组文章数量:1123202
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 |1 Answer
Reset to default 1When 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.
本文标签: class(Python) cant access attribute of an object unless I check nullitywhyStack Overflow
版权声明:本文标题:class - (Python) cant access attribute of an object unless I check nullity, why? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736100479a1903323.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
l1.nextNode
. – Klaus D. Commented yesterday