我无法理解暗示 a 的类型Coroutine。据我了解,当我们声明一个函数时,如下所示:
async def some_function(arg1: int, arg2: str) -> list:
...
Run Code Online (Sandbox Code Playgroud)
我们有效地声明了一个函数,它返回一个协程,当等待时,它返回一个列表。因此,输入提示的方法是:
f: Callable[[int, str], Coroutine[???]] = some_function
Run Code Online (Sandbox Code Playgroud)
但Coroutine泛型类型有 3 个参数!如果我们转到该typing.py文件,我们就可以看到它:
...
Coroutine = _alias(collections.abc.Coroutine, 3)
...
Run Code Online (Sandbox Code Playgroud)
还有一种Awaitable类型,从逻辑上讲,它应该是Coroutine只有一个泛型参数的父级(我想是返回类型):
...
Awaitable = _alias(collections.abc.Awaitable, 1)
...
Run Code Online (Sandbox Code Playgroud)
因此,也许以这种方式键入提示函数或多或少是正确的:
f: Callable[[int, str], Awaitable[list]] = some_function
Run Code Online (Sandbox Code Playgroud)
或者是吗?
所以,基本上,问题是:
Awaitable代替吗?Coroutineasync defCoroutine?它的用例是什么?例如我们有一个类:
class A:
def send(msg: bytes) -> None:
# implementation...
pass
def recv(n: int) -> bytes:
# implementation
pass
Run Code Online (Sandbox Code Playgroud)
和一个函数:
def a(obj, n: int) -> None:
received = obj.recv(n)
obj.send(received)
Run Code Online (Sandbox Code Playgroud)
很明显,不仅类的实例A可以作为参数传递obj,而且 的实例socket.socket,也许是其他已经实现的类的recv实例也可以作为send参数传递。
如何注释/键入提示obj参数,使其显示如下内容:
obj type must possess methods send and recv
send method must be of type Callable[[bytes], None]
recv method must be of type Callable[[int], bytes]
Run Code Online (Sandbox Code Playgroud) 与 一起工作时numba,我偶然发现了非常意想不到的行为。我创建了一个nb.njit函数,在其中尝试创建数组nb.typed.List,int8 numpy因此我尝试创建相应的numba类型。
nb.int8[:] # type of the list elements
Run Code Online (Sandbox Code Playgroud)
因此,我将此类型设置为nb.typed.Listvialsttype关键字。
l = nb.typed.List(lsttype=nb.int8[:]) # list of int8 numpy ndarrays
Run Code Online (Sandbox Code Playgroud)
我得到的是:
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function getitem>) found for signature:
>>> getitem(class(int8), slice<a:b>)
There are 16 candidate implementations:
- Of which 16 did not match due to:
Overload in function 'getitem': File: <built-in>: Line <N/A>.
With …Run Code Online (Sandbox Code Playgroud) 我有一个类,其中的方法只能运行一次。当然,可以很容易地使用人工has_executed = True/False标志来完成,但是如果您可以删除方法本身,为什么还要使用它呢? python鸭子式的语言,一切都是参考,bla-bla-bla,会出什么问题吗?
至少是这么想的。我实际上做不到:
class A:
def b(self):
print("empty")
self.__delattr__('b')
a = A()
a.b()
Run Code Online (Sandbox Code Playgroud)
提高AttributeError: b。然而,执行self.__getattribute__('b')returns <bound method A.b of <__main__.A object at 0x000001CDC6742FD0>>,这对我来说听起来很愚蠢:为什么 amethod与 an 不同attribute,因为 in 中的所有内容都python只是对对象的引用?而为什么我可以__getattribute__,却不能__delattr__?
重新定义也是如此。我可以轻松设置任何属性,但方法是不行的吗?
class A:
def b(self):
print("first")
self.__setattr__('b', lambda self: print(f"second"))
a = A()
a.b()
a.b()
Run Code Online (Sandbox Code Playgroud)
结果变成TypeError: <lambda>() missing 1 required positional argument: 'self'. 当然,这意味着现在python没有按预期使用点表示法。当然,self …
假设我们有一个数组:
let arr: [u8; 10] = [1,2,3,4,5,6,7,8,9,10];
Run Code Online (Sandbox Code Playgroud)
Rust 中是否有一个函数可以N从中选择随机元素而不重复?相当于python的random.sample功能。
当您执行时contacts.resolveUsername,结果始终包含id和access_hash。它给 API 用户带来了困惑,为什么你只能通过他们username或他们的对来解析用户/聊天/频道(id, access_hash)(例如,这里和这里)。
我尝试在telegram API 主页上搜索信息,但没有找到有关 的含义的信息access_hash。
我真的很困惑为什么你需要比id(或username)更多的东西。我想知道这件事的本质access_hash:
contacts.resolveUsername从账户 #1 拨打,然后从账户 #2 拨打,我得到的号码是否相同?)?我知道像这样的库pyrogram,telethon将其存储access_hash在本地sqlite数据库中。id这样,它们就可以调用需要同时使用和access_hash仅使用 的高级函数id。
python ×4
type-hinting ×2
types ×2
annotations ×1
coroutine ×1
duck-typing ×1
mtproto ×1
numba ×1
numpy ×1
pyrogram ×1
random ×1
rust ×1
std ×1
telegram ×1
telethon ×1