473,396 Members | 2,061 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,396 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 1873
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.