Movatterモバイル変換
[0]ホーム
Note:ThisUserString class from this module is available forbackward compatibility only. If you are writing code that does notneed to work with versions of Python earlier than Python 2.2, pleaseconsider subclassing directly from the built-instr typeinstead of usingUserString (there is no built-in equivalenttoMutableString).
This module defines a class that acts as a wrapper around stringobjects. It is a useful base class for your own string-like classes,which can inherit from them and override existing methods or add newones. In this way one can add new behaviors to strings.
It should be noted that these classes are highly inefficient comparedto real string or Unicode objects; this is especially the case forMutableString.
TheUserString module defines the following classes:
- classUserString([sequence])
- Class that simulates a string or a Unicode stringobject. The instance's content is kept in a regular string or Unicodestring object, which is accessible via thedata attribute ofUserString instances. The instance's contents are initiallyset to a copy ofsequence.sequence can be either aregular Python string or Unicode string, an instance ofUserString (or a subclass) or an arbitrary sequence which canbe converted into a string using the built-instr() function.
- classMutableString([sequence])
- This class is derived from theUserString above and redefinesstrings to bemutable. Mutable strings can't be used asdictionary keys, because dictionaries requireimmutable objects askeys. The main intention of this class is to serve as an educationalexample for inheritance and necessity to remove (override) the__hash__() method in order to trap attempts to use amutable object as dictionary key, which would be otherwise veryerror prone and hard to track down.
In addition to supporting the methods and operations of string andUnicode objects (see section2.2.6, ``StringMethods''),UserString instances provide the followingattribute:
- data
- A real Python string or Unicode object used to store the content of theUserString class.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭