How to implement a minimal class that behaves like a sequence in Python?

前端 未结 2 865
春和景丽
春和景丽 2020-12-19 04:25

I am looking for a sample minimal example of a class that mimics an immutable sequence in Python.

class MySequence()
    ...

a = MySequence()

len(a)

for i         


        
2条回答
  •  天涯浪人
    2020-12-19 04:48

    If you just want to be able to iterate over your sequence, you just need to implement the __iter__ method returning an iterable. The easiest way to do this is to create a generator using the yield statement.

    class MySequence(object):
        def __iter__(self):
            yield 1
            yield 2
            yield 3
    
    for x in MySequence():
        print x # prints 1, then 2, then 3
    

    However, this will not enable things like MySequence()[1]. For that you need to implement the __getitem__ method, and should probably implement __len__ as well.

    class MySequence(object):
        def __len__(self):
            return 3
    
        def __getitem__(self, key):
            if key == 0:
                return 1
            elif key == 1:
                return 2
            elif key == 2:
                return 3
            else:
                raise IndexError()
    
    s = new MySequence()
    
    for i in range(len(s)):
        print s[i] # prints 1, then 2, then 3
    
    for x in s:
        print x # prints 1, then 2, then 3
    

    Notice that I omitted __iter__. As long as __getitem__ raises an IndexError when you try to get a value that's out-of-bounds, Python can use it for iteration. (I could still include __iter__ if I wanted to be clearer, or wanted non-standard iteration behaviour.)

提交回复
热议问题