Creating list-like objects in Python


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # See <http://docs.python.org/ref/sequence-types.html>
  2. #
  3. # For a sequence, the allowable keys should be the integers k for which
  4. # 0 <= k < N where N is the length of the sequence, or slice objects,
  5. # which define a range of items.
  6.  
  7. class MySequence:
  8.  
  9. def __len__(self):
  10. """Called to implement the built-in function len(). Should
  11. return the length of the object, an integer >= 0. Also, an
  12. object that doesn't define a __nonzero__() method and whose
  13. __len__() method returns zero is considered to be false in a
  14. Boolean context."""
  15. pass
  16.  
  17. def __getitem__(self,key):
  18. """Called to implement evaluation of self[key]. For sequence
  19. types, the accepted keys should be integers and slice objects.
  20. Note that the special interpretation of negative indexes (if
  21. the class wishes to emulate a sequence type) is up to the
  22. __getitem__() method. If key is of an inappropriate type,
  23. TypeError may be raised; if of a value outside the set of
  24. indexes for the sequence (after any special interpretation of
  25. negative values), IndexError should be raised. For mapping
  26. types, if key is missing (not in the container), KeyError
  27. should be raised. Note: for loops expect that an IndexError
  28. will be raised for illegal indexes to allow proper detection of
  29. the end of the sequence."""
  30. pass
  31.  
  32. def __setitem__(self,key,value):
  33. """Called to implement assignment to self[key]. Same note as
  34. for __getitem__(). This should only be implemented for mappings
  35. if the objects support changes to the values for keys, or if
  36. new keys can be added, or for sequences if elements can be
  37. replaced. The same exceptions should be raised for improper key
  38. values as for the __getitem__() method."""
  39. pass
  40.  
  41. def __delitem__(self,key):
  42. """Called to implement deletion of self[key]. Same note as for
  43. __getitem__(). This should only be implemented for mappings if
  44. the objects support removal of keys, or for sequences if
  45. elements can be removed from the sequence. The same exceptions
  46. should be raised for improper key values as for the __getitem__
  47. () method."""
  48. pass
  49.  
  50. # __iter__ is not strictly required, it's only needed to implement
  51. # efficient iteration.
  52. def __iter__(self):
  53. """This method is called when an iterator is required for a
  54. container. This method should return a new iterator object that
  55. can iterate over all the objects in the container. For
  56. mappings, it should iterate over the keys of the container, and
  57. should also be made available as the method iterkeys()."""
  58. pass
  59.  
  60. # __contains__ isn't strictly required either, it's only needed to
  61. # implement the `in` operator efficiently.
  62. def __contains__(self,item):
  63. """Called to implement membership test operators. Should return
  64. true if item is in self, false otherwise. For mapping objects,
  65. this should consider the keys of the mapping rather than the
  66. values or the key-item pairs."""
  67. pass
  68.  
  69. # Mutable sequences only, provide the Python list methods.
  70. def append(self,item):
  71. pass
  72. def count(self):
  73. pass
  74. def index(self,item):
  75. pass
  76. def extend(self,other):
  77. pass
  78. def insert(self,item):
  79. pass
  80. def pop(self):
  81. pass
  82. def remove(self,item):
  83. pass
  84. def reverse(self):
  85. pass
  86. def sort(self):
  87. pass
  88.  
  89. # If you want to support addition and multiplication provide the
  90. # methods __add__(), __radd__(), __iadd__(), __mul__(), __rmul__()
  91. # and __imul__().

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.