这在某种程度上是学术性的,但不过.
Python语法禁止使用数字启动变量名称.但这可以像这样回避:
>>> globals()['1a'] = 1
>>> globals()['1a']
1
Run Code Online (Sandbox Code Playgroud)
同样地locals().
这是否意味着,Python实际上允许它,只是不是很明显?
编辑:
我的问题不是,是否允许.我知道,它在Python中是正式不允许的.问题是为什么我可以通过globals()直接解决来解决它,并且这会破坏其他规则或指导方针,甚至可能有一个很好的理由/应用程序来允许它.
我正在尝试使用 Python 创建一个 Discord 机器人,但是每当我在这里运行示例代码时:
import discord
client = discord.Client()
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.content.startswith('!hello'):
msg = 'Hello {0.author.mention}'.format(message)
await client.send_message(message.channel, msg)
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run('tokenhere')
Run Code Online (Sandbox Code Playgroud)
它返回错误:
Traceback (most recent call last):
File "<ipython-input-6-ea5a13e5703d>", line 1, in <module>
runfile('C:/Users/User/Pictures/rito_bot.py', wdir='C:/Users/User/Pictures')
File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 703, in runfile
execfile(filename, namespace)
File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, …Run Code Online (Sandbox Code Playgroud) 我正在 discord.py 库中寻找一种方法来返回已部署机器人的服务器的所有者。现在我只是对其进行了硬编码(即
if message.content == '!owner':
await message.channel.send('ownername#1234')
Run Code Online (Sandbox Code Playgroud)
),但是当所有者更改他们的名字时,我必须更改它,这似乎很麻烦。我已经查看了 discord.py 的权限系统,但它似乎没有这样做。有什么可以做到这一点吗?