是的,你可以用 __iter__
__iter__
class pair: def __init__(self, first, second): self.first = first self.second = second def __iter__(self): # Use tuple's iterator since it is the closest to our use case. return iter((self.first, self.second))