Kaydet (Commit) b8b6d3ef authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Let the world know that UserList is a MutableSequence.

üst 8284c4a7
"""A more or less complete user-defined wrapper around list objects.""" """A more or less complete user-defined wrapper around list objects."""
class UserList: import collections
class UserList(collections.MutableSequence):
def __init__(self, initlist=None): def __init__(self, initlist=None):
self.data = [] self.data = []
if initlist is not None: if initlist is not None:
...@@ -69,3 +71,5 @@ class UserList: ...@@ -69,3 +71,5 @@ class UserList:
self.data.extend(other.data) self.data.extend(other.data)
else: else:
self.data.extend(other) self.data.extend(other)
collections.MutableSequence.register(UserList)
...@@ -6,10 +6,11 @@ Note: string objects have grown methods in Python 1.6 ...@@ -6,10 +6,11 @@ Note: string objects have grown methods in Python 1.6
This module requires Python 1.6 or later. This module requires Python 1.6 or later.
""" """
import sys import sys
import collections
__all__ = ["UserString","MutableString"] __all__ = ["UserString","MutableString"]
class UserString: class UserString(collections.Sequence):
def __init__(self, seq): def __init__(self, seq):
if isinstance(seq, str): if isinstance(seq, str):
self.data = seq self.data = seq
...@@ -161,7 +162,9 @@ class UserString: ...@@ -161,7 +162,9 @@ class UserString:
def upper(self): return self.__class__(self.data.upper()) def upper(self): return self.__class__(self.data.upper())
def zfill(self, width): return self.__class__(self.data.zfill(width)) def zfill(self, width): return self.__class__(self.data.zfill(width))
class MutableString(UserString): collections.Sequence.register(UserString)
class MutableString(UserString, collections.MutableSequence):
"""mutable string objects """mutable string objects
Python strings are immutable objects. This has the advantage, that Python strings are immutable objects. This has the advantage, that
...@@ -230,6 +233,8 @@ class MutableString(UserString): ...@@ -230,6 +233,8 @@ class MutableString(UserString):
self.data *= n self.data *= n
return self return self
collections.MutableSequence.register(MutableString)
if __name__ == "__main__": if __name__ == "__main__":
# execute the regression test to stdout, if called as a script: # execute the regression test to stdout, if called as a script:
import os import os
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment