- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下功能:
def __eq__(self, other: object) -> Union[bool, NotImplemented]:
try:
for attr in ["name", "variable_type"]:
if getattr(self, attr) != getattr(other, attr):
return False
return True
except AttributeError:
return NotImplemented # Expression has type "Any"
我正在运行 mypy,它告诉我 NotImplemented 是“Any”类型,它显然不喜欢这种类型。
有没有更好的类型可以用来消除这个错误?还是我应该放一个 type: ignore
然后继续? (此外,我在返回类型中使用 NotImplemented
是否正确?)
最佳答案
它是NotImplementedType
。
type(NotImplemented)
# NotImplementedType
也就是说,你可以这样定义你的函数:
def foo(self, other: object) -> Union[bool, type(NotImplemented)]:
pass
现在,help(foo)
给出,
help(foo)
# Help on function foo in module __main__:
#
# foo(self, other:object) -> Union[bool, NotImplementedType]
顺便说一句,值得一提的是 NotImplemented
是一个用于非常特定目的的单例对象(它用于指示未在对象上定义特定操作)。您几乎从来不需要真正访问它的类型,而且据我所知,您可以从任何地方导入 NotImplementedType
。
我只是将 __eq__
定义为
def foo(self, other: object) -> bool:
pass
当返回NotImplemented
时,应该理解它不是一个有效的结果,而是相等比较无效的指示。
关于python - NotImplemented 的类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56615826/
我有以下功能: def __eq__(self, other: object) -> Union[bool, NotImplemented]: try:
我正在编写一个新的扩展类型,但我在设置数字运算(例如加法/减法/乘法)时遇到了问题。我已经设法设置了一些就地操作,而没有调用正常操作。 例如,我有这个功能: static PyObject * MyT
查看decimal.py,它在很多特殊的方法中使用了NotImplemented。例如 class A(object): def __lt__(self, a): return
我正在尝试使用 HippoMock 模拟接口(interface),以便在使用所述接口(interface)的类中使用。我构建了一个模拟对象并设置了 ExceptCallOverload,一切都可以正
我有一个用于一些插件式东西的基类,并且有一些方法是绝对需要实现的。 例如,我目前将基类中的那些声明为虚拟的 public virtual void Save { throw new NotIm
Python 有一个名为 NotImplemented 的单例。 . 为什么有人想要返回 NotImplemented 而不是引发 NotImplementedError异常(exception)?这
假设我定义了一个类 A 并且我不希望任何人在不逃避的情况下编写该类的不等式。 class A(): def __ne__(self, other): return NotImp
我最近偶然发现了 Python 的 NotImplemented 内置函数。经过一些阅读后,我现在确实明白了它的目的,但我不明白为什么它会评估为 True 作为 bool 值。下面的例子让我觉得这是一
从 python 3 中的 __eq__ 特殊方法返回 NotImplemented 的结果是什么(如果重要,则为 3.5)? 文档不清晰; only relevant text I found仅含糊
不要把苹果和橙子混在一起 问题 我正在使用 __eq__ 运算符和 NotImplemented 值。 我试图了解当 obj1.__eq__(obj2) 返回 NotImplemented 并且 ob
Python 有一个名为 NotImplemented 的单例。 . 为什么有人想要返回 NotImplemented 而不是提高 NotImplementedError异常(exception)?会
我正在使用我知道可以工作的存储帐户和帐户 key /信用:我有一个使用完全相同的连接字符串的 blob 容器,可以正常工作。 我正在有效地这样做: CloudStorageAccount.Parse(
为了回答另一个问题,我一直在研究 pandas 中的列乘法运算。 A = pd.DataFrame({'Col1' : [1, 2, 3], 'Col2' : [2, 3, 4]}) B = pd.D
我惊讶地发现 python(版本 3.2.2)拒绝 pickle 一个对象,因为它的字典包含对 Ellipsis 的引用。其他built-in constants , pickle 很高兴与 Fals
我正在尝试让一个非常简单的 Scrapy 爬虫工作。我从解析中收到 NotImplemented 错误 - 本质上与此相同的问题:Why does scrapy throw an error for
我有一个代数对象的类层次结构,它实现了特殊方法,例如 __mul__ 和 __add__ ,并使用多重继承。我以某种方式假设 Python (>= 3.5) 会遍历方法解析顺序 (mro) 来查找第一
这个问题是从 this question 的回答和讨论中提出来的.以下片段显示了问题的关键: >>> bool(NotImplemented) True 我的问题如下: 为什么决定 NotImplem
如果我有两个对象 A 和 B,我可以为 A 的 返回 NotImplemented >__iadd__ 方法,并让 B 使用它的 __radd__ 方法修改 A。 >>> class A(): ...
我尝试在 sympy 中求解 4 个微分方程组。我收到“NotImplementedError”。有解决办法吗? 我试图解决的颂歌集是: ![ODE]: /image/2U4BR.jpg 我尝试用数值
我正在尝试设置一个端点以将文件上传到 AWS S3 存储桶。我正在使用 Node.JS、Express、AWS S3 SDK 和 Multer 库。 这是我目前用于上传中间件的代码: export c
我是一名优秀的程序员,十分优秀!