473,466 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is it possible to have instance variables in subclasses of builtins?

I've recently used subclasses of the builtin str class to good effect.
However, I've been unable to do the following:

1) Call the constructor with a number of arguments other than
the number of arguments taken by the 'str' constructor.

2) Create and use instance variables (eg. 'self.x=1') in
the same way that I can in a 'normal' class.

As an example of what I mean, here's a short little session:
class a(str): .... def __init__(self, a, b):
.... str.__init__(a)
.... self.b = b
.... x = a("h", "g") Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: str() takes at most 1 argument (2 given)
(hmm, on reflection, I shouldn't have used 'a' as a
parameter to __init__--but that's merely bad
style, not the cause of the error :-) )

On the other hand, this example works:
class b(str): .... def __init__(self, x):
.... str.__init__(x)
.... x = b("h")
x 'h'


Is it possible to circumvent the above restrictions?

Thanks,
Ken
Jul 18 '05 #1
2 1325

The following might work for you:

from UserString import UserString
class b(UserString):
def __init__(self, x, y, z):
self.y=y
self.z=z
self.x=x
UserString.__init__(self, x)
return
x=b('h', 1, 2)
x.z 2 x.y 1 x.x 'h' len(x)
1

Uses old UserString class.

HTH,
Larry Bates
Syscon, Inc.
"Kenneth McDonald" <ke****************@sbcglobal.net> wrote in message
news:sl*******************************@g4.gateway. 2wire.net...
I've recently used subclasses of the builtin str class to good effect.
However, I've been unable to do the following:

1) Call the constructor with a number of arguments other than
the number of arguments taken by the 'str' constructor.

2) Create and use instance variables (eg. 'self.x=1') in
the same way that I can in a 'normal' class.

As an example of what I mean, here's a short little session:
class a(str): ... def __init__(self, a, b):
... str.__init__(a)
... self.b = b
... x = a("h", "g") Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: str() takes at most 1 argument (2 given)
(hmm, on reflection, I shouldn't have used 'a' as a
parameter to __init__--but that's merely bad
style, not the cause of the error :-) )

On the other hand, this example works:
class b(str): ... def __init__(self, x):
... str.__init__(x)
... x = b("h")
x 'h'


Is it possible to circumvent the above restrictions?

Thanks,
Ken

Jul 18 '05 #2
"Kenneth McDonald" <ke****************@sbcglobal.net> wrote in message
news:sl*******************************@g4.gateway. 2wire.net...
I've recently used subclasses of the builtin str class to good effect.
However, I've been unable to do the following:

1) Call the constructor with a number of arguments other than
the number of arguments taken by the 'str' constructor.

2) Create and use instance variables (eg. 'self.x=1') in
the same way that I can in a 'normal' class.

As an example of what I mean, here's a short little session:
class a(str): ... def __init__(self, a, b):
... str.__init__(a)
... self.b = b
... x = a("h", "g") Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: str() takes at most 1 argument (2 given)
The problem is that "str" is an immutable type. Therefore, to change the
constructor, you need to override the __new__ method. See
http://www.python.org/2.2.1/descrintro.html#__new__

Here's an example:
class two(str): def __new__(cls, a, b):
return str.__new__(cls, a)
def __init__(self, a, b):
self.b = b
z = two("g", "h")
z 'g' z.b

'h'

Note that both arguments (a and b) get passed to both the __new__ and
__init__ methods, and each one just ignores the one it doesn't need.
--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Hoffman | last post by:
I was compelled to write this today for some reason. builtins = """__import__ abs basestring bool callable chr classmethod cmp compile complex delattr dict dir divmod enumerate eval execfile...
0
by: Dave | last post by:
Hi everyone, (I already posted this to the VS.NET IDE news group without any responses, so I'm attempting one more time in this group) The issue I'm having is occuring in the IDE of VS.NET...
0
by: Dave | last post by:
Hi everyone, (I already posted this to the VS.NET IDE news group without any responses, so I'm attempting one more time in this group) The issue I'm having is occuring in the IDE of VS.NET...
10
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
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...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.