I'm writing a program and want to create a class that is derived from the "str" base type. When I do so, however, I have problems with the __init__ method. When I run the code below, it will call my new __init__ method when there is zero or one (value) parameter. However, if I try to pass two parameters or a named parameter, then it dies with an error indicating that it's actually trying to call the "str:__init__" method instead.
Any help would be appreciated. Thanks! $ python -d Python 2.5 (r25:51908, Mar 13 2007, 08:13:14) [GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin Type "help", "copyright", "credits" or "license" for more information. $ ./problem.py xstr:__init__: None None None xstr:__init__: test None None E ====================================================================== ERROR: test_init (__main__.XStringTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "./problem.py", line 22, in test_init self.assertEquals('test', xstr('test', 'test:')) TypeError: str() takes at most 1 argument (2 given) ---------------------------------------------------------------------- Ran 1 test in 0.003s FAILED (errors=1) #! /usr/bin/python -t ############################################################################### class xstr(str): def __init__(self, value=None, xlate=None, xpart=None): print 'xstr:__init__:',value,xlate,xpart self.xlate = xlate self.xpart = xpart str.__init__(self, value) ############################################################################### import unittest from test import test_support class XStringTest(unittest.TestCase): def test_init(self): self.assertEquals('', xstr()) self.assertEquals('test', xstr('test')) self.assertEquals('test', xstr('test', 'test:')) self.assertEquals('', xstr(xlate='test:')) ############################################################################### if __name__ == '__main__': unittest.main() -- http://mail.python.org/mailman/listinfo/python-list