python - while 循环如何处理类属性
问题描述
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, value):
if self.head is None:
self.head = Node(value)
return
# Move to the tail (the last node)
node = self.head
while node.next:
node = node.next
node.next = Node(value)
return
我对 while 循环语句在这种情况下的工作方式有点困惑。只要条件为真,While 循环就可以工作。我不确定在这种情况下while循环条件将如何返回真或假,有人可以解释一下。谢谢!
解决方案
node.next
评估为一个值,然后将该值评估为布尔值。
具体来说, ifnode.next = None
和bool(None) == False
循环中断。否则bool(<Node object>) == True
,循环继续。
推荐阅读
- php - 图像在 Web 服务器根目录之外时的 HTML 图像绝对路径
- android - 改造中的表单数据请求失败
- java - Android Studio 中的图像资源变得奇怪
- javascript - 正在显示 HTML 图像文本而不是图像
- apache-spark - 在 spark-submit 上获取多条消息“寻求最早并重置分区主题分区的偏移量”
- html - 如何修复文本不对齐?
- html - 将查看区域之间的图像与左右文本对齐
- python - 为什么使用函数 scipy.stats.median_absolute_deviation 计算的 MAD 与我所做的函数不同?
- r - 如何使用 Reticulate 在 R shiny App 中导入 Python 函数以供下载 App
- amazon-dynamodb - 在 DynamoDB 中创建 Web 流量日志的最佳方法是什么?