python - 在 Python 中检查子类
问题描述
我目前正在使用电报 cli,现在,我必须通过这几行代码检查用户是否在特定组内
with TelegramClient(name, api_id, api_hash) as client:
result = client(functions.messages.CheckChatInviteRequest(hash=hash))
我得到的结果是print(type(result))
:
<class 'telethon.tl.types.ChatInvite'>
如果我不在组内
<class 'telethon.tl.types.ChatInviteAlready'>
如果我已经在里面的话。
现在,我想做的是:
if type(result) == telethon.tl.types.ChatInvite:
print('You are not inside the group')
但显然这不起作用,它给了我这个错误NameError: name 'telethon' is not defined
我如何检查子类?
谢谢 :)
解决方案
如果可以 import ChatInvite
,请执行以下操作:
from telethon.tl.types import ChatInvite
result = get_result(...)
if isinstance(result, ChatInvite):
print('You are not inside the group')
推荐阅读
- jsf - 第二个数据表中的 p:commandButton 不起作用
- sql - 如何计算仅返回最新项目的所有子查询结果?
- r - 连接错误点 R 的折线图
- python - 从文件中提取 3 列数据并按数字顺序排列
- java - 无法使用我的 procfile 更改编码
- angular6 - matTextareaAutosize 在 Angular Material 6.4.2 中不起作用
- vba - 如何使用 VBA 删除 word 文档中的空白页?
- java - 使用 throw 语句实现 try-catch 块
- c++ - Boost Spirit QI :在替代函数内具有序列的元组上的自动规则推导
- docker - 我可以告诉 dockerfile 将文件写入主机吗?