471,337 Members | 923 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

subclassing Python types

I have read that you can derive from the base classes such as str,
list, dict.

I guess this would look like:

def MyString(str):
def MyList(list):
def MyDict(dict):
How do you access the data that is contained in the super class?

Aug 30 '07 #1
10 1307
On Aug 30, 8:00 pm, zzbba...@aol.com wrote:
I have read that you can derive from the base classes such as str,
list, dict.

I guess this would look like:

def MyString(str):
def MyList(list):
def MyDict(dict):
You mean

class MyString(str):
....
How do you access the data that is contained in the super class?
The same way that you access plain strings, lists or dicts: use the
methods of the super class.

HTH

PS: it is not so often a good idea to derive a class from these types.

--
Arnaud
Aug 30 '07 #2
On Aug 30, 12:13 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
On Aug 30, 8:00 pm, zzbba...@aol.com wrote:
I have read that you can derive from the base classes such as str,
list, dict.
I guess this would look like:
def MyString(str):
def MyList(list):
def MyDict(dict):

You mean

class MyString(str):
...
How do you access the data that is contained in the super class?

The same way that you access plain strings, lists or dicts: use the
methods of the super class.
I don't know what name I would use to call a method:
class MyString(str):
def __init__(strInput):
????? = strInput
Aug 30 '07 #3
zz******@aol.com wrote:
I have read that you can derive from the base classes such as str,
list, dict.

I guess this would look like:

def MyString(str):
def MyList(list):
def MyDict(dict):

Well, replace 'def' with 'class' and you're right.
How do you access the data that is contained in the super class?
This way:
>>class MyList(list):
.... def do_something(self):
.... self.append(3)
.... print self
....
>>l = MyList((1, 2))
l
[1, 2]
>>l.do_something()
[1, 2, 3]
That is: Whenever you want to refer to the value refer to self (that is,
refer to the instance of your class).

/W
Aug 30 '07 #4
On Aug 30, 12:18 pm, Wildemar Wildenburger
<lasses_w...@klapptsowieso.netwrote:
zzbba...@aol.com wrote:
I have read that you can derive from the base classes such as str,
list, dict.
I guess this would look like:
def MyString(str):
def MyList(list):
def MyDict(dict):

Well, replace 'def' with 'class' and you're right.
How do you access the data that is contained in the super class?

This way:
>>class MyList(list):
... def do_something(self):
... self.append(3)
... print self
...
>>l = MyList((1, 2))
>>l
[1, 2]
>>l.do_something()
[1, 2, 3]

That is: Whenever you want to refer to the value refer to self (that is,
refer to the instance of your class).

/W
Ok, thanks.

So it's:
class MyString(str):
def __init__(self,strInput):
self = strInput

Aug 30 '07 #5
On Aug 30, 8:24 pm, zzbba...@aol.com wrote:
On Aug 30, 12:18 pm, Wildemar Wildenburger

<lasses_w...@klapptsowieso.netwrote:
zzbba...@aol.com wrote:
I have read that you can derive from the base classes such as str,
list, dict.
I guess this would look like:
def MyString(str):
def MyList(list):
def MyDict(dict):
Well, replace 'def' with 'class' and you're right.
How do you access the data that is contained in the super class?
This way:
>>class MyList(list):
... def do_something(self):
... self.append(3)
... print self
...
>>l = MyList((1, 2))
>>l
[1, 2]
>>l.do_something()
[1, 2, 3]
That is: Whenever you want to refer to the value refer to self (that is,
refer to the instance of your class).
/W

Ok, thanks.

So it's:
class MyString(str):
def __init__(self,strInput):
self = strInput
No. Well in this case it seems to work as the method str.__new__ is
called first which does what you intend to do in your example. But try
this:
>>class Upper(str):
.... def __init__(self, s):
.... self = s.upper()
....
>>Upper('camelot')
'camelot'

Doesn't work (should be 'CAMELOT', shouldn't it?)!
To understand why it doesn't behave as you expect, and how to achieve
what you want, see "Overriding the __new__ method" in GvR's article
"Unifying types and classes in Python 2.2" (http://www.python.org/
download/releases/2.2.3/descrintro/#__new__).

HTH

--
Arnaud
Aug 30 '07 #6
zz******@aol.com wrote:
On Aug 30, 12:13 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
>On Aug 30, 8:00 pm, zzbba...@aol.com wrote:
>>I have read that you can derive from the base classes such as str,
list, dict.
I guess this would look like:
def MyString(str):
def MyList(list):
def MyDict(dict):
You mean

class MyString(str):
...
>>How do you access the data that is contained in the super class?
The same way that you access plain strings, lists or dicts: use the
methods of the super class.

I don't know what name I would use to call a method:
class MyString(str):
def __init__(strInput):
????? = strInput

I think your understanding of Python needs to advance a little before
you start trying to do something like this. It's the __new__() method
you need to be overriding, not the __init__() method.
>>class MyString(str):
.... def __new__(cls, val):
.... return str.__new__(cls, val)
....
>>s = MyString("1234")
s
'1234'
>>type(s)
<class '__main__.MyString'>
>>>
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 30 '07 #7
On Aug 30, 8:42 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
[...]
"Unifying types and classes in Python 2.2" (http://www.python.org/
download/releases/2.2.3/descrintro/#__new__).
That's:

http://www.python.org/download/relea...intro/#__new__

(damn google groups ;)
--
Arnaud
Aug 30 '07 #8
zz******@aol.com wrote:
So it's:
class MyString(str):
def __init__(self,strInput):
self = strInput
That doesn't quite work. Assigning to "self" only reassigns the name inside the
function. It does not replace the object.

Instead, call the .__init__() method on str.

class MyString(str):
def __init__(self, strInput):
str.__init__(self, strInput)
# ... other stuff

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Aug 30 '07 #9
On Aug 30, 8:44 pm, Robert Kern <robert.k...@gmail.comwrote:
zzbba...@aol.com wrote:
So it's:
class MyString(str):
def __init__(self,strInput):
self = strInput

That doesn't quite work. Assigning to "self" only reassigns the name inside the
function. It does not replace the object.

Instead, call the .__init__() method on str.
That won't do much as strings are immutable objects...
As pointed out by Steve Holden and me, str.__new__ is the way.
--
Arnaud
Aug 30 '07 #10
Arnaud Delobelle wrote:
On Aug 30, 8:44 pm, Robert Kern <robert.k...@gmail.comwrote:
>zzbba...@aol.com wrote:
>>So it's:
class MyString(str):
def __init__(self,strInput):
self = strInput
That doesn't quite work. Assigning to "self" only reassigns the name inside the
function. It does not replace the object.

Instead, call the .__init__() method on str.

That won't do much as strings are immutable objects...
As pointed out by Steve Holden and me, str.__new__ is the way.
I suspect the OP is analogous to the drunk who was looking for his key
next to a lamp post. When a helper became exasperated with the lack of
results he started to question the drunk about where he'd dropped the
key. "Over there" says the drunk, pointing about twenty yards away.
"Then why are you looking over here?". Comes the reply:
"Because you can't see a bloody thing where I dropped it."

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 30 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by GrelEns | last post: by
2 posts views Thread by Fuzzyman | last post: by
11 posts views Thread by Iker Arizmendi | last post: by
2 posts views Thread by BJörn Lindqvist | last post: by
6 posts views Thread by gregory lielens | last post: by
reply views Thread by rosydwin | last post: by

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.