Problem Inheriting from "str" Class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bcwhite@pobox.com

    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__.XStri ngTest)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
    File "./problem.py", line 22, in test_init
    self.assertEqua ls('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,x part
    self.xlate = xlate
    self.xpart = xpart
    str.__init__(se lf, value)

    ############### ############### ############### ############### ############### ####

    import unittest
    from test import test_support

    class XStringTest(uni ttest.TestCase) :

    def test_init(self) :
    self.assertEqua ls('', xstr())
    self.assertEqua ls('test', xstr('test'))
    self.assertEqua ls('test', xstr('test', 'test:'))
    self.assertEqua ls('', xstr(xlate='tes t:'))

    ############### ############### ############### ############### ############### ####

    if __name__ == '__main__':
    unittest.main()

  • Gabriel Genellina

    #2
    Re: Problem Inheriting from "str&qu ot; Class

    En Wed, 13 Jun 2007 03:01:16 -0300, bcwhite@pobox.c om <bcwhite@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.h tml>

    --
    Gabriel Genellina

    Comment

    Working...