【发布时间】:2020-05-20 01:55:36
【问题描述】:
所以这里我对容器类有一些定义:
class Box:
def __init__(self, x1: int) -> None:
self._x1 = x1
@property
def x1(self) -> int:
return self._x1
然后在我的第二个函数中,我将传入这个 Box 对象
class Foo:
def __init__(self, bar_1: Box) -> None
那么现在,在某些情况下,bar_1 也可以是 None...如果我将 none 传递给类 Foo,则会出现错误:
"Foo" has incompatible type "None"; expected "Box"
那么我怎么能绕过这个呢? python是否允许同时进行2种类型提示?
class Foo:
def __init__(self, bar_1: Box or None) -> None
我需要帮助!
【问题讨论】:
-
这能回答你的问题吗? How to specify multiple return types using type-hints。不要让标题迷惑你。这对参数也有效
标签: python type-hinting