Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > trouble subclassing str

Thread Tools

trouble subclassing str

Donn Cave
Posts: n/a
In article <(E-Mail Removed) .com>,
"Paul McGuire" <(E-Mail Removed)> wrote:
> This reminds me of some maddening O-O discussions I used to
> have at a former place of employment, in which one developer cited
> similar behavior for not having Square inherit from Rectangle - calling
> Square.setWidth() would have to implicitly call setHeight() and vice
> versa, in order to maintain its squarishness, and thereby broke Liskov.
> I withdrew from the debate, citing lack of context that would have
> helped resolve how things should go. At best, you can *probably* say
> that both inherit from Shape, and can be drawn, have an area, a
> bounding rectangle, etc., but not either inherits from the other.

This Squares and Rectangles issue sounds debatable in a
language like C++ or Java, where it's important because
of subtype polymorphism. In Python, does it matter?
As a user of Square, I'm not supposed to ask about its
parentage, I just try to be clear what's expected of it.
There's no static typing to notice whether Square is a
subclass of Rectangle, and if it gets out that I tried
to discover this issubclass() relationship, I'll get a
lecture from folks on comp.lang.python who suspect I'm
confused about polymorphism in Python.

This is a good thing, because as you can see it relieves
us of the need to debate abstract principles out of context.
It doesn't change the real issues - Square is still a lot
like Rectangle, it still has a couple of differences, and
the difference could be a problem in some contexts designed
for Rectangle - but no one can fix that. If you need Square,
you'll implement it, and whether you choose to inherit from
Rectangle is left as a matter of implementation convenience.

Donn Cave, Removed)
Reply With Quote
Bengt Richter
Posts: n/a
On 23 Jun 2005 21:27:20 -0700, "Paul McGuire" <(E-Mail Removed)> wrote:

>Dang, that class should be:
>class PaddedStr(str):
> def __new__(cls,s,l,padc=' '):
> if l > len(s):
> s2 = "%s%s" % (s,padc*(l-len(s)))
> return str.__new__(cls,s2)
> else:
> return str.__new__(cls,s)

Or you could write

>>> class PaddedStr2(str):

... def __new__(cls,s,l,padc=' '):
... return str.__new__(cls, s+padc*(l-len(s)))

Which gives

>>> print '>%s<' % PaddedStr2('xxx',5,'.')

>>> print '>%s<' % PaddedStr2('xxx',3,'.')

>>> print '>%s<' % PaddedStr2('xxx',2,'.')


(Taking advantage of multipliers <=0 working like 0 for strings):

>>> for i in xrange(-3,4): print '%2s: >%s<'% (i, 'xxx'+'.'*i)

-3: >xxx<
-2: >xxx<
-1: >xxx<
0: >xxx<
1: >xxx.<
2: >xxx..<
3: >xxx...<

Bengt Richter
Reply With Quote

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
subclassing str not1xor1 (Alessandro) Python 9 11-11-2010 04:29 AM
It is fun.the result of str.lower(str()) Sullivan WxPyQtKinter Python 5 03-09-2006 08:09 AM
subclassing str Thomas Lotze Python 4 09-14-2004 02:06 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM
what's the deference between str=null and str=" " ???????? David Java 2 08-03-2003 04:10 PM