473,398 Members | 2,403 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,398 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 1380
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: GrelEns | last post by:
hello, i wonder if this possible to subclass a list or a tuple and add more attributes ? also does someone have a link to how well define is own iterable object ? what i was expecting was...
2
by: Fuzzyman | last post by:
I recently went through a bit of a headache trying to subclass string.... This is because the string is immutable and uses the mysterious __new__ method rather than __init__ to 'create' a string....
11
by: Iker Arizmendi | last post by:
Hello all, I've defined a class in a C extension module that is (or rather, I would like to be) a subclass of another class. I do this by simply setting the tp_base pointer of my subclass's...
2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
6
by: gregory lielens | last post by:
Hello, I am currently writing python bindings to an existing C++ library, and I just encountered a problem that I hope has been solved by more experienced python programmers: A C++ class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.