471,616 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

Problem Inheriting from "str" Class

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

Jun 13 '07 #1
1 1805
En Wed, 13 Jun 2007 03:01:16 -0300, bc*****@pobox.com <bc*****@pobox.com>
escribió:
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>

--
Gabriel Genellina

Jun 13 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by ScottyO | last post: by
5 posts views Thread by kungfoobar | last post: by
8 posts views Thread by John Nagle | last post: by
3 posts views Thread by Ge Chunyuan | last post: by
2 posts views Thread by jkazoo | last post: by
1 post views Thread by XIAOLAOHU | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.