[Python-bugs-list] [ python-Bugs-468061 ] str ignored in str subclass (original) (raw)
noreply@sourceforge.net noreply@sourceforge.net
Thu, 04 Oct 2001 13:52:35 -0700
- Previous message: [Python-bugs-list] [ python-Bugs-467583 ] Restricted Execution docs broken ref
- Next message: [Python-bugs-list] [ python-Bugs-468061 ] __str__ ignored in str subclass
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Bugs item #468061, was opened at 2001-10-04 13:52 You can respond by visiting: http://sourceforge.net/tracker/?func=detail&atid=105470&aid=468061&group_id=5470
Category: Type/class unification Group: Python 2.2 Status: Open Resolution: None Priority: 5 Submitted By: Tim Peters (tim_one) Assigned to: Guido van Rossum (gvanrossum) Summary: str ignored in str subclass
Initial Comment:
From c.l.py; looks messy (e.g., if PyObjectString didn't ignore octetstring.str, the call to str() in the latter would just recurse forever; and he's right that b2a_hex requires a genuine string; and e.g.
return b2a_hex(self + "")would be mighty obscure).
----Original Message----- From: Andreas.Trawoeger Sent: Thursday, October 04, 2001 7:10 AM To: python-list Subject: Python 2.2a4: Problems with overloading str in subclasses of str
I would like to write a class that behaves like a string only with the difference that it displays the string in hex. In Python 2.2a4 this should be possible by subclassing str and overloading repr and str :
from binascii import b2a_hex
class octetstring(str): def repr(self): return b2a_hex(str(self)) def str(self): return b2a_hex(str(self))
o = octetstring('A')
print "o.repr():",o.repr() print "o.str() :",o.str() print "str(o) :",str(o) print "o :",o
The result is pretty strange:
o.repr(): 41 o.str() : 41 str(o) : A o : A
I'm not shure what's the exact reason for this problem. Either str isn't used for str() and print. Or I'm trapped in an chicken egg problem: I have to convert self to an string (otherwise b2a_hex won't work) to do this Python should use str (which is the method I'm changing).
cu andreas
You can respond by visiting: http://sourceforge.net/tracker/?func=detail&atid=105470&aid=468061&group_id=5470
- Previous message: [Python-bugs-list] [ python-Bugs-467583 ] Restricted Execution docs broken ref
- Next message: [Python-bugs-list] [ python-Bugs-468061 ] __str__ ignored in str subclass
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]