由于某些原因,send_message在我的Discord机器人上运行不正常,我无论如何也无法找到它.
import asyncio
import discord
client = discord.Client()
@client.async_event
async def on_message(message):
author = message.author
if message.content.startswith('!test'):
print('on_message !test')
await test(author, message)
async def test(author, message):
print('in test function')
await client.send_message(message.channel, 'Hi %s, i heard you.' % author)
client.run("key")
Run Code Online (Sandbox Code Playgroud)
on_message !test
in test function
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\indit\AppData\Roaming\Python\Python36\site-packages\discord\client.py", line 223, in _run_event
yield from coro(*args, **kwargs)
File "bot.py", line 15, in on_message
await test(author, message)
File "bot.py", line 21, in test
await client.send_message(message.channel, …Run Code Online (Sandbox Code Playgroud) Python 3.6.5和mypy 0.600
我写了代码:
from typing import List
class Animal():
pass
class Dog(Animal):
def __init__(self) -> None:
super()
def bark(self) -> None:
pass
class Cat(Animal):
def __init__(self) -> None:
super()
def meow(self) -> None:
pass
arr1: List[Dog] = [Dog(), Dog()]
arr2: List[Animal] = [Dog(), Dog()]
# error: Incompatible types in assignment (expression has type "List[Dog]", variable has type "List[Animal]")
arr3: List[Animal] = arr1
Run Code Online (Sandbox Code Playgroud)
我不明白,为什么我有一个错误“赋值中的不兼容类型”和变量“ arr3”。狗是从动物继承的类。例如,变量'arr2'没有错误。
当迭代异构序列(例如,包含T1和类型的元素T2)时,mypy 推断目标变量具有类型(或和object之间共享的另一个基本类型,例如,如果元素是和):T1T2float11.2
xs = [1, "1"]
for x in xs:
reveal_type(x) # note: Revealed type is 'builtins.object*'
Run Code Online (Sandbox Code Playgroud)
推断出的类型不是更有意义吗Union[T1, T2]?然后,如果 和 都T1具有T2公共基类所缺少的一些公共属性,则将允许循环体访问该属性,而不会产生令人讨厌的强制转换或 isinstance 断言。
为什么 mypy 推断出单个共享基类型而不是此处Union?
在围棋之旅说以下内容:
您只能使用接收器声明一个方法,该接收器的类型与方法在同一个包中定义.您不能使用接收器声明一个方法,该接收器的类型在另一个包中定义(包括内置类型,如int).
除了避免每个人都建立自己的方法之外int,还有其他原因string吗?我用Google搜索过,但找不到任何引用它的内容.