subclassing Python types

Z

zzbbaadd

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?
 
A

Arnaud Delobelle

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.
 
Z

zzbbaadd

You mean

class MyString(str):
...


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
 
W

Wildemar Wildenburger

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:
.... 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
 
Z

zzbbaadd

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:... 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
 
J

James Stroud

I don't know what name I would use to call a method:


class MyString(str):
def __init__(strInput):
????? = strInput

You need to read up on subclassing builtin types and/or describe what
you want to achieve. str is an immutable type, and so you must return an
instance from __new__. Read this:

http://www.python.org/download/releases/2.2.3/descrintro/

And here is an example that might get you started (assuming you really
want to do this):

class StupidDefaultString(str):
default = "I'm a Default!"
success = "42"
error = "Not a default, don't bother."
def __new__(cls, arg=None):
if arg is None:
arg = cls.default
return str.__new__(cls, arg)
def __str__(self):
if self == self.default:
return self.success
else:
return self.error

py> s = StupidDefaultString('Bob')
py> s
'Bob'
py> print s
Not a default, don't bother.
py> s = StupidDefaultString()
py> s
"I'm a Default!"
py> print s
42


James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
A

Arnaud Delobelle

Well, replace 'def' with 'class' and you're right.
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).

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:
.... def __init__(self, s):
.... self = s.upper()
....'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
 
S

Steve Holden

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.
.... def __new__(cls, val):
.... return str.__new__(cls, val)
....
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 -------------
 
R

Robert Kern

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
 
A

Arnaud Delobelle

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.
 
S

Steve Holden

Arnaud said:
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 -------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top