On 3/07/2006 10:01 PM, Jon Clements wrote: > John Machin wrote: > (snip) >> You have already been told: you don't need "self.<what>", you just write >> "self" ... self *is* a reference to the instance of the mystr class that >> is being operated on by the substr method. >> > (snip) > > I get that; let me clarify why I asked again. > > As far as I'm aware, the actual representation of a string needn't be > the same as its 'physical' value. ie, a string could always appear in > uppercase ('ABCD'), while stored as 'aBcd'. If I need to guarantee that > substr always returned from the physical representation and not the > external appearance, how would I do this? Or, would self, always return > internal representation, (if so, how would I get external appearance?). > > Or I could be talking complete _beep_ - in which case I apologise. > > Jon. > The external appearance of an object is produced by repr and str methods. These transform the internal value into a human-readable (and Python-compilable, in the case of repr) format. When you are subclassing, these methods would normally be inherited.
Let's transpose your question into the realm of floats. A float is usually a 64-bit gizmoid with about 53 bits of mantissa, a sign bit, and the rest is for the exponent. The external representations are character strings like '3.3333333333333333e-021'. You want to subclass float so that you can add handy methods like squared(). You would like it to be as simple as: def squared(self): return self * self There are two possibilities: (a) It just works; e.g. print myfloat(1.1).squared() produces 1.21 (b) It doesn't work, you get some mysterious exception like TypeError: can't multiply sequence by non-int which you decode as meaning that it doesn't like you trying to multiply two character strings together, and you have to code the return expression as self.__internal__ * self.__internal__ or something like that. 1. Which of these possibilities do you think is more useful/elegant/plausible? 2. [Stop me if you've heard this one before] When you try it out, what happens? 3. Have you ever seen any indication in the manuals, tutorials, books, code published online, etc etc as to which possibility has been implemented? HTH, John -- http://mail.python.org/mailman/listinfo/python-list