Python 3.5类型提示不会导致错误
发布于 2021-01-29 14:57:33
键入:PEP 484 –键入提示。
我想测试它,但是它没有按预期工作。
import typing
class BankAccount:
def __init__(self, initial_balance: int = 0) -> None:
self.balance = initial_balance
def deposit(self, amount: int) -> None:
self.balance += amount
def withdraw(self, amount: int) -> None:
self.balance -= amount
def overdrawn(self) -> bool:
return str(self.balance < 0)
my_account = BankAccount(15)
my_account.withdraw(5)
print(type(my_account.overdrawn()))
结果是:
<class 'str'>
我期待一个错误,因为我期望布尔作为回报。我在python:3.5(docker)和local上测试了它。我是否想念一些东西以使其起作用?这种键入是否在运行时不起作用(例如python
app.py)?
关注者
0
被浏览
62
1 个回答