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

前端 未结 2 872
春和景丽
春和景丽 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:55

    Adding to @Jeremy's answer: A popular check that a value is a general sequence is to use isinstance(value, collections.Sequence).

    In order to make this true for your type it needs to inherit from collections.Sequence, and this actually provides the iterator (and some other useful functions) as mixins, as long as you provide the __len__ and __getitem__ functions.

    Borrowing from @Jeremy's answer, an example class would look like:

    import collections
    class MySequence(collections.Sequence):
        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()
    

    Examples of usage:

    s = 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
    
    print isinstance(s, collections.Sequence) # prints True
    
    print 1 in s # prints True
    
    print list(reversed(s)) # prints [3, 2, 1]
    

提交回复
热议问题