Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is it possible to have instance variables in subclasses of builtins?

Reply
Thread Tools

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

 
 
Kenneth McDonald
Guest
Posts: n/a
 
      06-15-2004
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
 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      06-15-2004

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" <> wrote in message
news:. 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



 
Reply With Quote
 
 
 
 
Russell Blau
Guest
Posts: n/a
 
      06-16-2004
"Kenneth McDonald" <> wrote in message
news:. 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class variables per subclasses, defined in the superclass Gareth Adams Ruby 2 12-11-2007 09:14 PM
Why does list.__getitem__ return a list instance for subclasses ofthe list type? dackz Python 0 02-06-2007 04:44 PM
Custom Taglib problems - instead of a single instance per page, I have a single instance per application. chris brat Java 1 05-10-2006 11:16 AM
class variables for subclasses tuple alainpoint@yahoo.fr Python 6 03-08-2006 01:38 PM
Can I have std::vector store these subclasses mixed? Eric Lilja C++ 6 02-26-2005 12:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57