我在 Python 中发现的奇怪 class 声明
Strange class declaration I found in Python
所以我试图在 LeetCode 上做一些编程问题,我看到了一个我不太熟悉的 class 声明方法。原来是这样:
class Solution:
def romanToInt(self, s: str) -> int:
我习惯的是:
class Solution:
def romanToInt(self, s):
两者有区别吗?
它 type annotation 没有运行时意义(在没有其他库如 Pydantic 的情况下),但允许进行一些静态类型检查。
在这种情况下,它只是意味着 s
的类型是一个字符串,并且方法 returns 和 int
所以我试图在 LeetCode 上做一些编程问题,我看到了一个我不太熟悉的 class 声明方法。原来是这样:
class Solution:
def romanToInt(self, s: str) -> int:
我习惯的是:
class Solution:
def romanToInt(self, s):
两者有区别吗?
它 type annotation 没有运行时意义(在没有其他库如 Pydantic 的情况下),但允许进行一些静态类型检查。
在这种情况下,它只是意味着 s
的类型是一个字符串,并且方法 returns 和 int