gpt4 book ai didi

python - 当作为参数传递给 range() 时,如何告诉 PyCharm 类实例可以通过 __index__ 方法解释为整数?

转载 作者:行者123 更新时间:2023-12-01 07:16:34 24 4
gpt4 key购买 nike

考虑以下示例:

import random


class Class:
def __index__(self):
return random.randint(-100, 100)


c = Class()
print(range(c, c, c))

它运行正确并打印类似 range(11, -29, -77) 的内容。但 PyCharm 警告我,在作为参数传递给 range 的每个 c 上,预期类型“int”,获得“Class” 3 次。我想通过向 PyCharm 解释将 Class 实例作为参数传递给 range 来消除此警告。我试图将 Class 声明为 numbers.Integral 的子类,以表明 Class 确实实现了整数类型,但它没有帮助,修改了示例:

import random
import numbers


class Class(numbers.Integral):
def __index__(self):
return random.randint(-100, 100)

__int__ = __index__ # it doesn't help either

__abs__ = None # they all must be overloaded because they are abstract in numbers.Integral
__add__ = None
__and__ = None
__ceil__ = None
__eq__ = None
__floor__ = None
__floordiv__ = None
__invert__ = None
__le__ = None
__lshift__ = None
__lt__ = None
__mod__ = None
__mul__ = None
__neg__ = None
__or__ = None
__pos__ = None
__pow__ = None
__radd__ = None
__rand__ = None
__rfloordiv__ = None
__rlshift__ = None
__rmod__ = None
__rmul__ = None
__ror__ = None
__round__ = None
__rpow__ = None
__rrshift__ = None
__rshift__ = None
__rtruediv__ = None
__rxor__ = None
__truediv__ = None
__trunc__ = None
__xor__ = None


c = Class()
print(range(c, c, c))

警告仍然存在。您知道如何处理吗?

最佳答案

根据[Python 3.Docs]: Data model - object.__index__(self) :

Note: In order to have a coherent integer type class, when __index__() is defined __int__() should also be defined, and both should return the same value.

但从问题中可以看出,这并不是新信息。下面,我尝试了不同的替代方案。

1。代码更改

code00.py:

#!/usr/bin/env python3

import sys
import random
from typing import SupportsInt


def _randint():
return random.randint(-100, 100)


class C0:
def __index__(self):
return _randint()


class C1:
def __index__(self):
return _randint()

__int__ = __index__


class C2(SupportsInt):
def __index__(self):
return _randint()

__int__ = __index__


class C3(int):
def __index__(self):
return _randint()

__int__ = __index__


def main():
c0 = C0()
c1 = C1()
c2 = C2()
c3 = C3()

print(c1.__class__.__name__, list(range(int(c1), c1)))
print(c2.__class__.__name__, list(range(int(c2), c2)))
print(c3.__class__.__name__, list(range(int(c3), c3)))
print(c0.__class__.__name__, list(range(int(c0), c0)))


if __name__ == "__main__":
print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
main()
print("\nDone.")

Img0

注释:

  • 显然,PyCharm代码检查器没有进行足够深入的检查
  • 将对象显式转换为int。就我个人而言,我认为这是一种解决方法,就我而言,警告的缺失并不能证明它是合理的。无论如何,它不适用于 C0 对象,这会引发异常
  • 扩展int就可以了。但同样,我不知道是否值得这样做只是为了摆脱警告

2。 PyCharm 设置

Img1

禁用Python -> 类型检查器检查。正如所见,它也能达到目的。但它也隐藏了一些真正的错误(例如,int(c0)不再突出显示,但它是错误的)。因此,就我而言,这弊大于利。
也许您可以将其保留为事件状态,但请调整其设置以缩小其效果。更多详情请参阅[JetBrains]: Code inspections .

关于python - 当作为参数传递给 range() 时,如何告诉 PyCharm 类实例可以通过 __index__ 方法解释为整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57922380/

24 4 0
文章推荐: actionscript-3 - Flash - 播放一次影片剪辑
文章推荐: jquery - 将
  • 聚焦在
  • Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com