Where does str class represent its data?
Klaas
mike.klaas at gmail.com
Fri Jul 13 19:56:12 EDT 2007
On Jul 11, 4:37 pm, Miles <semantic... at gmail.com> wrote:
> Since strings are immutable, you need to override the __new__ method.
> Seehttp://www.python.org/download/releases/2.2.3/descrintro/#__new__
In case this isn't clear, here is how to do it:
In [1]: class MyString(str):
...: def __new__(cls, value):
...: return str.__new__(cls, value.lower())
In [2]: s = MyString('Hello World')
In [3]: s
Out[3]: 'hello world'
Note that this will not do fancy stuff like automatically call
__str__() methods. If you want that, call str() first:
In [5]: class MyString(str):
...: def __new__(cls, value):
...: return str.__new__(cls, str(value).lower())
-Mike
More information about the Python-list
mailing list