473,216 Members | 1,508 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,216 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 1375
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.