Source From Here
QuestionI have the following code in python 3:
- class Position:
- def __init__(self, x: int, y: int):
- self.x = x
- self.y = y
- def __add__(self, other: Position) -> Position:
- return Position(self.x + other.x, self.y + other.y)
HowTo
If you are using Python 3.10 or later, it just works. As of today (2019), in 3.7+ you must turn this feature on using a future statement (from __future__ import annotations). In Python 3.6 or below, use a string.
I guess you got this exception:
- NameError: name 'Position' is not defined
Python 3.7+: from __future__ import annotations
Python 3.7 introduces PEP 563: postponed evaluation of annotations. A module that uses the future statement from __future__ import annotations will store annotations as strings automatically:
- from __future__ import annotations
- class Position:
- def __add__(self, other: Position) -> Position:
- ...
Python <3.7: use a string
According to PEP 484, you should use a string instead of the class itself:
- class Position:
- ...
- def __add__(self, other: 'Position') -> 'Position':
- ...
PHP multiple file upload
回覆刪除How to fetch data from database in PHP
text to audio converter
ttk combobox
Extract text from image Python
PHP query string
Save emoji in MySQL
Python bouncing ball
QR code generator PHP