Re: trouble subclassing str

2005-06-25 Thread Bengt Richter
On 23 Jun 2005 21:27:20 -0700, "Paul McGuire" <[EMAIL PROTECTED]> 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: >

Re: trouble subclassing str

2005-06-24 Thread Donn Cave
In article <[EMAIL PROTECTED]>, "Paul McGuire" <[EMAIL PROTECTED]> 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.setWid

Re: trouble subclassing str

2005-06-24 Thread Paul McGuire
Look at the related post, on keeping key-key pairs in a dictionary. Based on our discussion in this thread, I created a subclass of dict called SymmetricDict, that, when storing symDict["A"] = 1, implicitly saves the backward looking symDict[1] = "A". I chose to inherit from dict, in part just to

Re: trouble subclassing str

2005-06-24 Thread Donn Cave
In article <[EMAIL PROTECTED]>, "Paul McGuire" <[EMAIL PROTECTED]> wrote: [ ... lots of interesting discussion removed ... ] > Most often, I see "is-a" confused with "is-implemented-using-a". A > developer decides that there is some benefit (reduced storage, perhaps) > of modeling a zip code usi

Re: trouble subclassing str

2005-06-24 Thread Paul McGuire
>From purely Python terms, there is a distinction that one of these classes (PaddedStr) is immutable, while the other is not. Python only permits immutable objects to act as dictionary keys, so this would one thing to differentiate these two approaches. But on a more abstract, implementation-inde

Re: trouble subclassing str

2005-06-24 Thread Kent Johnson
Donn Cave wrote: > Left unexplained is ``true "is-a" relationships''. Sounds > like an implicit contradiction -- you can't implement > something that truly is something else. Without that, and > maybe a more nuanced replacement for "is-implemented-using-a", > I don't see how you could really be s

Re: trouble subclassing str

2005-06-23 Thread Paul McGuire
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) -- Paul -- http://mail.python.org/mailman/listin

Re: trouble subclassing str

2005-06-23 Thread John Machin
Brent wrote: > I'd like to subclass the built-in str type. For example: You'd like to build this weird-looking semi-mutable object as a perceived solution to what problem? Perhaps an alternative is a class of objects which have a "key" (your current string value) and some data attributes? Mayb

Re: trouble subclassing str

2005-06-23 Thread Donn Cave
In article <[EMAIL PROTECTED]>, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Thu, 23 Jun 2005 12:25:58 -0700, Paul McGuire wrote: > > > But if you are subclassing str just so that you can easily print your > > objects, look at implementing the __str__ instance method on your > > class. Reser

Re: trouble subclassing str

2005-06-23 Thread Steven D'Aprano
On Thu, 23 Jun 2005 12:25:58 -0700, Paul McGuire wrote: > But if you are subclassing str just so that you can easily print your > objects, look at implementing the __str__ instance method on your > class. Reserve inheritance for true "is-a" relationships. Often, > inheritance is misapplied when

Re: trouble subclassing str

2005-06-23 Thread Paul McGuire
My first thought is "make sure that subclassing str is really what you want to do." Here is a place where I have a subclass of str that really is a special kind of str: class PaddedStr(str): def __new__(cls,s,l,padc=' '): if l > len(s): s2 = "%s%s" % (s,padc*(l-len(s)))

trouble subclassing str

2005-06-23 Thread Brent
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data if __name__ == '__main__': s1 = MyString("some text", 100) -- but I get the error: Traceback (most rec