`reveal_type`输出中星号的含义及`int`与`int*`的区别
reveal_type Output & int vs int* Hey there! Let's break down what that asterisk means in mypy's reveal_type output, and clarify the difference between int and int* in your code examples.
What the Asterisk Signifies
In mypy, that trailing * in the revealed type is a marker for modifiable l-values. Put simply, it means the expression points to a memory location you can overwrite later. Mypy uses this to signal that the type here isn't "fixed" at compile time—you could change the value's type in subsequent code, so mypy treats it with more flexibility and gives you a heads-up about this dynamic nature.
Breaking Down the Code Examples
Let's walk through each case to see how int and int* differ:
reveal_type(1)→builtins.int:
The literal1is an immutable r-value—once created, it can't be changed. Its type is completely static and guaranteed to beint, so there's no asterisk needed here.reveal_type(bla[0])→builtins.int*:bla[0]refers to the first element in your list, which is a modifiable position. You could easily write something likebla[0] = "suddenly a string"later to change its type entirely. Mypy recognizes this mutability and adds the*to warn you that this value's type isn't guaranteed to stayintat runtime.reveal_type(bla[0] * 2)→builtins.int:
This is the result of multiplyingbla[0]by 2—this creates a brand-new, immutable integer value. Even ifbla[0]gets modified later, this calculation's result is a fixedintthat can't be altered, so no asterisk is present.
内容的提问来源于stack exchange,提问作者Nova

