简体   繁体   English

如何使用函数返回的列表,作为 Python 中的函数参数

[英]How to use returned list from function, as function args in Python

    @property
    def randomcolours(self):
        return [int(x * 255) for x in colorsys.hsv_to_rgb(random.random(), 1, 1)]

    @tasks.loop(seconds=3, count=15)
    async def rainbow_embed(self, _, t, d):
        await self.bot.ini.edit(embed=discord.Embed(title=t, description=d, colour=discord.Colour.from_rgb(*self.randomcolours())))

I am trying to pass the 3 returned values from randomcolours as the arguments in the colour kwarg but I am getting a TypeError: 'list' object is not callable , how can I do this?我试图将randomcolours的 3 个返回值作为colour kwarg 中的参数传递,但我收到TypeError: 'list' object is not callable ,我该怎么做?

randomcolours is a @property , so an expression like self.randomcolours will call the function and evaluate to the list. randomcolours是一个@property ,所以喜欢的表达self.randomcolours会调用该函数并评价到列表中。 Thus, when you call it like self.randomcolours() , you're actually calling the list returned from the property.因此,当您像self.randomcolours()一样调用它时,您实际上是在调用从属性返回的列表。 It's not possible to call a list, so you get the error.无法调用列表,因此您会收到错误消息。

How to solve:怎么解决:

  • don't call the list: discord.Colour.from_rgb(*self.randomcolours)不要调用列表: discord.Colour.from_rgb(*self.randomcolours)
  • or don't make randomcolours a property或者不要让randomcolours成为一个属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM