【发布时间】:2019-07-08 20:35:54
【问题描述】:
这是一个关于如何检测对象是否为空的通用问题。我将一个变量声明为一个对象:
description = discord.Embed()
通过一个可能会也可能不会向对象传递参数的方法,即:
def my_function(x, y, z):
...some code goes here...
if x == "some variable":
description = discord.Embed(title="X", desc="Y + z")
return description
else:
description = discord.Embed()
return description
我希望仅在不为空时显示描述:
if description: client.send_message(message.channel, embed=description)
但是,上面的代码似乎不起作用,并且无论它是否为空,都会显示我的消息。我该怎么办?
【问题讨论】:
-
试试这个:if !description: client.send_message(message.channel, embed=description)
-
discord.Embed()返回什么? -
“空”对象定义不明确。仅仅因为你没有传递任何参数并不意味着没有一些默认值可以被使用。
-
@MateusMartins 你确定这不是
syntax error吗? -
您可能想要设置
description = None。