473,246 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 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 1865
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: matthurne | last post by:
I'm doing a chapter 12 exercise from Accelerated C++ ... writing a string-like class which stores its data in a low-level way. My class, called Str, uses a char array and length variable. I've...
1
by: ScottyO | last post by:
Hello, I have some legacy C++ code that was re-written as a managed class. I made a C++ class library with it and I want to use it in my C# program. I created a reference to it and it...
2
by: john | last post by:
In a query I have this expression: Expr1: "ZH"+Str() It should give me ZH1234 but it adds a space between the 2 parts so I get ZH 1234. What can I do to prevent this? Thanks, john
5
by: kungfoobar | last post by:
I want to have a str with custom methods, but I have this problem: class myStr(str): def hello(self): return 'hello '+self s=myStr('world') print s.hello() # prints 'hello world'...
8
by: John Nagle | last post by:
The Python documentation for "str" says "str() : Return a string containing a nicely printable representation of an object." However, there's no mention of the fact that "str" of a Unicode...
3
by: Ge Chunyuan | last post by:
hi Group: Once use ActivePython latest version for Python 2.5.1. I happened to find function "str" is not callable, but it is available for Python 2.5. Can anybody give some comments about it?...
2
by: shivapadma | last post by:
The following is the front end code for searching , it takes data and searches from the database by the second code. FRONT END CODE: <html> <head> <meta http-equiv="Content-Language"...
2
by: jkazoo | last post by:
so I’m trying to create a class that inherits from str, but I want to run some code on the value on object init. this is what I have: class Path(str): def __init__( self, path ): clean =...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.