blob: 1475197b3aa75e3356d5b225ff8c24db64faa3f8 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
Georg Brandl116aa622007-08-15 14:28:22 +00002
Georg Brandl116aa622007-08-15 14:28:22 +00003: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
16This module defines a class that acts as a wrapper around list objects. It is a
17useful base class for your own list-like classes, which can inherit from them
18and override existing methods or add new ones. In this way one can add new
19behaviors to lists.
20
21The :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 Drake2e748782007-09-04 17:33:11 +000027 list, which is accessible via the :attr:`data` attribute of
28 :class:`UserList`
Georg Brandl116aa622007-08-15 14:28:22 +000029 instances. The instance's contents are initially set to a copy of *list*,
Fred Drake2e748782007-09-04 17:33:11 +000030 defaulting to the empty list ``[]``. *list* can be any iterable, for
31 example a real Python list or a :class:`UserList` object.
Georg Brandl116aa622007-08-15 14:28:22 +000032
33In addition to supporting the methods and operations of mutable sequences (see
34section :ref:`typesseq`), :class:`UserList` instances provide the following
35attribute:
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
44offer a constructor which can be called with either no arguments or one
45argument. List operations which return a new sequence attempt to create an
46instance of the actual implementation class. To do so, it assumes that the
47constructor can be called with a single parameter, which is a sequence object
48used as a data source.
49
50If a derived class does not wish to comply with this requirement, all of the
51special methods supported by this class will need to be overridden; please
52consult the sources for information about the methods which need to be provided
53in that case.
54
Georg Brandl116aa622007-08-15 14:28:22 +000055
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
73This module defines a class that acts as a wrapper around string objects. It is
74a useful base class for your own string-like classes, which can inherit from
75them and override existing methods or add new ones. In this way one can add new
76behaviors to strings.
77
78It should be noted that these classes are highly inefficient compared to real
Georg Brandlf6945182008-02-01 11:56:49 +000079string or bytes objects; this is especially the case for
Georg Brandl116aa622007-08-15 14:28:22 +000080:class:`MutableString`.
81
82The :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 Brandlf6945182008-02-01 11:56:49 +000091 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 Brandl116aa622007-08-15 14:28:22 +000094
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 Brandlf6945182008-02-01 11:56:49 +0000106In addition to supporting the methods and operations of bytes and string
Georg Brandl116aa622007-08-15 14:28:22 +0000107objects (see section :ref:`string-methods`), :class:`UserString` instances
108provide the following attribute:
109
110
111.. attribute:: MutableString.data
112
Georg Brandlf6945182008-02-01 11:56:49 +0000113 A real Python string or bytes object used to store the content of the
Georg Brandl116aa622007-08-15 14:28:22 +0000114 :class:`UserString` class.
115