Overriding set methods in Python

前端 未结 2 1345
死守一世寂寞
死守一世寂寞 2021-01-03 13:43

I want to create a custom set that will automatically convert objects into a different form for storage in the set (see Using a Python Dictionary as a key non-nested) for ba

2条回答
  •  遥遥无期
    2021-01-03 14:26

    In Python 2.6:

    import collections
    print collections.MutableSet.__abstractmethods__
    # prints:
    # frozenset(['discard', 'add', '__iter__', '__len__', '__contains__'])
    

    subclass collections.MutableSet and override the methods in the list above.

    the update method itself is very easy, given that the bare minimum above is implemented

    def update(self, iterable):
        for x in iterable:
            self.add(x)
    

提交回复
热议问题