首页 > 解决方案 > 在 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 我如何检查子类?

谢谢 :)

标签: pythonclasstypestelethon

解决方案


如果可以 import ChatInvite,请执行以下操作:

from telethon.tl.types import ChatInvite

result = get_result(...)

if isinstance(result, ChatInvite): 
    print('You are not inside the group')

推荐阅读