Subclassing str object

2011-08-31 Thread Yaşar Arabacı
Hİ, I originally posted my question to here: http://stackoverflow.com/q/7255655/886669 Could you people please look at it and enlighten me a little bit? I would appreciate an answer either from here or at stackoverflow. Thanks in advance. -- http://yasar.serveblog.net/ --

Re: Subclassing str object

2011-08-31 Thread Yaşar Arabacı
I made a class like this (I shortened it just to show the point), what do you think about it, do you think it is the python way of subclassing str (or unicode in this case) # -*- coding:utf-8 -*-class kelime(unicode): def __init__(self,sozcuk): self._sozcuk = sozcuk def

Re: Subclassing str object

2011-08-31 Thread Terry Reedy
On 8/31/2011 7:43 AM, Yaşar Arabacı wrote: Hİ, I originally posted my question to here: http://stackoverflow.com/q/7255655/886669 Could you people please look at it and enlighten me a little bit? I would appreciate an answer either from here or at stackoverflow. I believe two people already

Re: Subclassing str object

2011-08-31 Thread Ian Kelly
2011/8/31 Yaşar Arabacı yasar11...@gmail.com: I made a class like this (I shortened it just to show the point), what do you think about it, do you think it is the python way of subclassing str (or unicode in this case) You don't need the _sozcuk attribute at all here. It's just the same

Re: Subclassing str object

2011-08-31 Thread Yaşar Arabacı
of unicode's 31 Ağustos 2011 20:11 tarihinde Ian Kelly ian.g.ke...@gmail.com yazdı: 2011/8/31 Yaşar Arabacı yasar11...@gmail.com: I made a class like this (I shortened it just to show the point), what do you think about it, do you think it is the python way of subclassing str (or unicode

Re: Subclassing str object

2011-08-31 Thread Ian Kelly
2011/8/31 Yaşar Arabacı yasar11...@gmail.com: @Ian: Thanks for you comments. I indeed didn't need the _sozcuk attribute at all, so I deleted it. My class's addition and multiplication works without overwriting __add__ and __mul__ because, this class uses unicode's __add__ and __mul__ than

Re: subclassing str

2010-11-10 Thread Mark Wooding
Lawrence D'Oliveiro l...@geek-central.gen.new_zealand writes: In message 87lj52kwln.fsf@metalzone.distorted.org.uk, Mark Wooding wrote: One option is to implement a subclass which implements the additional protocol. This is why I think object orientation ruins your ability to think

Re: subclassing str

2010-11-10 Thread Lawrence D'Oliveiro
In message 87lj52kwln.fsf@metalzone.distorted.org.uk, Mark Wooding wrote: One option is to implement a subclass which implements the additional protocol. This is why I think object orientation ruins your ability to think properly. For “protocol” read “function”. If you want to implement

Re: subclassing str

2010-11-10 Thread not1xor1 (Alessandro)
Il 09/11/2010 03:18, Lawrence D'Oliveiro ha scritto: How exactly does a.f(b, c) save time over f(a, b, c) unfortunately in real world you have: objId = objId.method(args) vs. objId = moduleName.method(objId, args) I know you can use from moduleName import *, but IMHO that

Re: subclassing str

2010-11-10 Thread Chris Rebert
On Wed, Nov 10, 2010 at 8:14 PM, not1xor1 (Alessandro) @libero.it wrote: Il 09/11/2010 03:18, Lawrence D'Oliveiro ha scritto: How exactly does    a.f(b, c) save time over     f(a, b, c) unfortunately in real world you have: objId = objId.method(args) vs. objId =

Re: subclassing str

2010-11-09 Thread Mark Wooding
rantingrick rantingr...@gmail.com writes: One thing i love about Python is the fact that it can please almost all the religious paradigm zealots with it's multiple choice approach to programming. However some of the features that OOP fundamentalists hold dear in their heart are not always

Re: subclassing str

2010-11-08 Thread Lawrence D'Oliveiro
In message 5dlbo.1024$w8@twister2.libero.it, not1xor1 (Alessandro) wrote: I'm already using plain functions, but thought that wrapping most of them in a str subclass would let me save some time and yield cleaner and more manageable code How exactly does a.f(b, c) save time over

Re: subclassing str

2010-11-08 Thread rantingrick
On Nov 8, 8:18 pm, Lawrence D'Oliveiro l...@geek- central.gen.new_zealand wrote: In message 5dlbo.1024$w8@twister2.libero.it, not1xor1 (Alessandro) wrote: I'm already using plain functions, but thought that wrapping most of them in a str subclass would let me save some time and yield

Re: subclassing str

2010-11-07 Thread Chris Rebert
/library/userdict.html#UserString.UserString But you should also consider whether your additions absolutely *must* be methods. Merely instead defining some functions that take strings as parameters is obviously a simpler, and probably more performant, approach. If you insist on subclassing str

Re: subclassing str

2010-11-07 Thread not1xor1 (Alessandro)
to modify my script accordingly I'm already using plain functions, but thought that wrapping most of them in a str subclass would let me save some time and yield cleaner and more manageable code If you insist on subclassing str, there's no such hook; you'll have to override all the methods

subclassing str

2010-11-06 Thread not1xor1 (Alessandro)
Hi, I'd like to know what is the best way to subclass str I need to add some new methods and that each method (both new and str ones) return my new type For instance I've seen I can do: class mystr(str): def between(self, start, end): i = self.index(start) + len(start) j =

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: return

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,

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 using an

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 see

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.setWidth()

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

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

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

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

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