簡體   English   中英

有人可以解釋下面這個單鏈接的最后一行代碼嗎? <"節點數據:%s>" % self.data

[英]Can someone explain this singly linked last line of code below? <"Node data: %s>" % self.data

class Node:

    def _init_(self, data, next_node = None):
        self.data = data
        self.next_node = next_node

    def _rept_self(self):
 
        return "<Node data: %s>" % self.data

有人可以向我解釋最后一行代碼嗎?

這是Python中字符串格式化的老方法,靈感來自C語法。 %s 是一個占位符,代表一個字符串,放在 % 后面的變量將用來代替 %s。

更直觀的語法是使用 fstring,如果將 f 放在字符串開頭之前,變量可以寫在大括號中:

return f"<Node data: {self.data}>"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM