Problem Inheriting from "str" Class

B

bcwhite

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()
 
G

Gabriel Genellina

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.

__init__ is rather useless for immutable types. You have to override
__new__ instead.
See <http://docs.python.org/ref/customization.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top