首页 > 解决方案 > 如何在类中为关键字参数提供预定义值?(Python)

问题描述

这是另一个愚蠢的问题:我想给我的结果键一个预定义的值,在这个例子中,“是”将获得值 A,然后“否”将获得值 B,如果我有第三个结果,它将获得值 C 等等on,我不想自己添加值,我希望类自动添加它。我该怎么做?

class bettingPool():
    def init(self, mesageId, bookie, betText, stake, **outcome):
        self._messageId = mesageId
        self._bookie = bookie
        self._betText = betText
        self._stake = stake
        self._outcome = outcome

我必须如何制作我的对象:

betPool1 = bettingPool("1", "player1", "will it work?", 20, yes=A, no=B)

我想如何制作我的对象:

betPool1 = bettingPool("1", "player1", "will it work?", 20, yes, no) <-  the class would assign the value A and B to "yes" and "no" automatically.

标签: keyword-argument

解决方案


推荐阅读