相关疑难解决方法(0)

Python 3.5类型提示不会导致错误

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)?

python type-hinting python-3.x

5
推荐指数
2
解决办法
1127
查看次数

标签 统计

python ×1

python-3.x ×1

type-hinting ×1