LJ Archive

Listing 2. Custom Class Iterations

import random
class RandSeq:
    def __init__(self, seq):
        self.data = seq

    def __iter__(self):
        return self

    def __next__(self):
        return random.choice(self.data)

>>> from randseq import RandSeq
>>> for eachItem in RandSeq(('rock', 'paper',
    'scissors')):
>>>     print eachItem
scissors
scissors
rock
paper
paper
scissors
    :
LJ Archive