Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | :mod:`UserList` --- Class wrapper for list objects |
| 4 | ================================================== |
| 5 | |
| 6 | .. module:: UserList |
| 7 | :synopsis: Class wrapper for list objects. |
| 8 | |
| 9 | |
| 10 | .. note:: |
| 11 | |
| 12 | This module is available for backward compatibility only. If you are writing |
| 13 | code that does not need to work with versions of Python earlier than Python 2.2, |
| 14 | please consider subclassing directly from the built-in :class:`list` type. |
| 15 | |
| 16 | This module defines a class that acts as a wrapper around list objects. It is a |
| 17 | useful base class for your own list-like classes, which can inherit from them |
| 18 | and override existing methods or add new ones. In this way one can add new |
| 19 | behaviors to lists. |
| 20 | |
| 21 | The :mod:`UserList` module defines the :class:`UserList` class: |
| 22 | |
| 23 | |
| 24 | .. class:: UserList([list]) |
| 25 | |
| 26 | Class that simulates a list. The instance's contents are kept in a regular |
Fred Drake | 2e74878 | 2007-09-04 17:33:11 +0000 | [diff] [blame] | 27 | list, which is accessible via the :attr:`data` attribute of |
| 28 | :class:`UserList` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | instances. The instance's contents are initially set to a copy of *list*, |
Fred Drake | 2e74878 | 2007-09-04 17:33:11 +0000 | [diff] [blame] | 30 | defaulting to the empty list ``[]``. *list* can be any iterable, for |
| 31 | example a real Python list or a :class:`UserList` object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
| 33 | In addition to supporting the methods and operations of mutable sequences (see |
| 34 | section :ref:`typesseq`), :class:`UserList` instances provide the following |
| 35 | attribute: |
| 36 | |
| 37 | |
| 38 | .. attribute:: UserList.data |
| 39 | |
| 40 | A real Python list object used to store the contents of the :class:`UserList` |
| 41 | class. |
| 42 | |
| 43 | **Subclassing requirements:** Subclasses of :class:`UserList` are expect to |
| 44 | offer a constructor which can be called with either no arguments or one |
| 45 | argument. List operations which return a new sequence attempt to create an |
| 46 | instance of the actual implementation class. To do so, it assumes that the |
| 47 | constructor can be called with a single parameter, which is a sequence object |
| 48 | used as a data source. |
| 49 | |
| 50 | If a derived class does not wish to comply with this requirement, all of the |
| 51 | special methods supported by this class will need to be overridden; please |
| 52 | consult the sources for information about the methods which need to be provided |
| 53 | in that case. |
| 54 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | |
| 56 | :mod:`UserString` --- Class wrapper for string objects |
| 57 | ====================================================== |
| 58 | |
| 59 | .. module:: UserString |
| 60 | :synopsis: Class wrapper for string objects. |
| 61 | .. moduleauthor:: Peter Funk <pf@artcom-gmbh.de> |
| 62 | .. sectionauthor:: Peter Funk <pf@artcom-gmbh.de> |
| 63 | |
| 64 | |
| 65 | .. note:: |
| 66 | |
| 67 | This :class:`UserString` class from this module is available for backward |
| 68 | compatibility only. If you are writing code that does not need to work with |
| 69 | versions of Python earlier than Python 2.2, please consider subclassing directly |
| 70 | from the built-in :class:`str` type instead of using :class:`UserString` (there |
| 71 | is no built-in equivalent to :class:`MutableString`). |
| 72 | |
| 73 | This module defines a class that acts as a wrapper around string objects. It is |
| 74 | a useful base class for your own string-like classes, which can inherit from |
| 75 | them and override existing methods or add new ones. In this way one can add new |
| 76 | behaviors to strings. |
| 77 | |
| 78 | It should be noted that these classes are highly inefficient compared to real |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 79 | string or bytes objects; this is especially the case for |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | :class:`MutableString`. |
| 81 | |
| 82 | The :mod:`UserString` module defines the following classes: |
| 83 | |
| 84 | |
| 85 | .. class:: UserString([sequence]) |
| 86 | |
| 87 | Class that simulates a string or a Unicode string object. The instance's |
| 88 | content is kept in a regular string or Unicode string object, which is |
| 89 | accessible via the :attr:`data` attribute of :class:`UserString` instances. The |
| 90 | instance's contents are initially set to a copy of *sequence*. *sequence* can |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 91 | be an instance of :class:`bytes`, :class:`str`, :class:`UserString` (or a |
| 92 | subclass) or an arbitrary sequence which can be converted into a string using |
| 93 | the built-in :func:`str` function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
| 95 | |
| 96 | .. class:: MutableString([sequence]) |
| 97 | |
| 98 | This class is derived from the :class:`UserString` above and redefines strings |
| 99 | to be *mutable*. Mutable strings can't be used as dictionary keys, because |
| 100 | dictionaries require *immutable* objects as keys. The main intention of this |
| 101 | class is to serve as an educational example for inheritance and necessity to |
| 102 | remove (override) the :meth:`__hash__` method in order to trap attempts to use a |
| 103 | mutable object as dictionary key, which would be otherwise very error prone and |
| 104 | hard to track down. |
| 105 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 106 | In addition to supporting the methods and operations of bytes and string |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | objects (see section :ref:`string-methods`), :class:`UserString` instances |
| 108 | provide the following attribute: |
| 109 | |
| 110 | |
| 111 | .. attribute:: MutableString.data |
| 112 | |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 113 | A real Python string or bytes object used to store the content of the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 | :class:`UserString` class. |
| 115 | |