python 3.5 的新功能之一是从该项目中获得启发的类型提示。
键入: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()))
Run Code Online (Sandbox Code Playgroud)
结果是:
<class 'str'>
Run Code Online (Sandbox Code Playgroud)
我期待一个错误,因为我期望布尔作为回报。我在python:3.5(docker)和local上测试了它。我是否想念一些东西以使其起作用?这种键入是否在运行时不起作用(例如python app.py)?