相关疑难解决方法(0)

明星拆包自己的课程

我想知道是否可以使用自己的类进行星形解包,而不仅仅是使用listtuple.

class Agent(object):
    def __init__(self, cards):
        self.cards = cards
    def __len__(self):
        return len(self.cards)
    def __iter__(self):
        return self.cards
Run Code Online (Sandbox Code Playgroud)

并且能够写作

agent = Agent([1,2,3,4])
myfunc(*agent)
Run Code Online (Sandbox Code Playgroud)

但我得到:

TypeError: visualize() argument after * must be a sequence, not Agent
Run Code Online (Sandbox Code Playgroud)

为了使拆包成为可能,我必须实施哪些方法?

python class python-3.x iterable-unpacking

6
推荐指数
1
解决办法
763
查看次数

标签 统计

class ×1

iterable-unpacking ×1

python ×1

python-3.x ×1