小编shi*_*ufa的帖子

无法在 exec 中等待

嗨,我试图让我的不和谐机器人做我在我的不和谐客户端中输入的内容,我想使用 exec() + 这只是为了测试和实验,所以它是否不安全并不重要。

我的代码的一部分:

import discord

client = discord.Client()

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('2B: '):
        exec(message.content[4:])   # <--- here is the exec()
    .
    .
    .
Run Code Online (Sandbox Code Playgroud)

但这是我输入时的错误,

2B: await client.send_message(message.channel, 'please stay quiet -.-')
Run Code Online (Sandbox Code Playgroud)

错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Shiyon\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "C:\Users\Shiyon\Desktop\dm_1.py", line 12, in on_message
    exec(message.content[4:])
  File "<string>", line 1
    await client.send_message(message.channel, 'please stay quiet -.-')
               ^
SyntaxError: …
Run Code Online (Sandbox Code Playgroud)

python python-3.x discord discord.py

3
推荐指数
1
解决办法
2427
查看次数

"class"vs"class of class"__get__和__set__?

问:为什么hh.a = 2不是我所期望的但ba = 2是好的?

这是代码:

class D:
    def __init__(self):
        print('this is init')
        self.name = ''

    def __get__(self,instance,owner):
        print('getting')
        return self.name


    def __set__(self,instance,value):
        print('setting')
        self.name = value
        return self.name
Run Code Online (Sandbox Code Playgroud)

和...

class hh:
    a = D()

b = hh()
Run Code Online (Sandbox Code Playgroud)

当我设置hh.a = 2时它不会调用print('setting')但是ba = 2是好的.你能解释一下吗?

python descriptor python-3.x

2
推荐指数
1
解决办法
80
查看次数

标签 统计

python ×2

python-3.x ×2

descriptor ×1

discord ×1

discord.py ×1