Returns the value of this tensor as a standard Python number. ```python exec="true" source="above" session="tensor" result="python" t = Tensor(42) print(t.item()) ```
(self)
| 308 | return self._buffer().as_memoryview().cast(self.dtype.base.fmt, self.shape) |
| 309 | |
| 310 | def item(self) -> PyConst: |
| 311 | """ |
| 312 | Returns the value of this tensor as a standard Python number. |
| 313 | |
| 314 | ```python exec="true" source="above" session="tensor" result="python" |
| 315 | t = Tensor(42) |
| 316 | print(t.item()) |
| 317 | ``` |
| 318 | """ |
| 319 | assert self.numel() == 1, "must have one element for item" |
| 320 | return self.data()[(0,) * len(self.shape)] |
| 321 | |
| 322 | # NOTE: list[Any] because return type is recursive (list[list[...]] for higher dimensions) |
| 323 | def tolist(self) -> PyConst|list[Any]: |