[Python-bugs-list] [ python-Bugs-468061 ] str ignored in str subclass (original) (raw)
noreply@sourceforge.net noreply@sourceforge.net
Thu, 11 Oct 2001 19:44:19 -0700
- Previous message: [Python-bugs-list] [ python-Bugs-470040 ] ParseTuple t# vs subclasses
- Next message: [Python-bugs-list] [ python-Bugs-467384 ] provide a documented serialization func
- 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: Tim Peters (tim_one) 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
Comment By: Tim Peters (timone) Date: 2001-10-11 19:44
Message: Logged In: YES user_id=31435
Bug 470040 has been fixed, so you'll be able to pass instances of str subclasses directly to b2a_hex (which uses the buffer interface to obtain the value; buffer methods simply weren't getting inherited; OTOH, I'm not sure this is such a great thing, since I'm not sure a str subclass in Python can override the buffer methods now inherited from str!).
Comment By: Tim Peters (tim_one) Date: 2001-10-10 15:52
Message: Logged In: YES user_id=31435
The business about b2a_hex not accepting an object of a subclass of str is now the subject of bug 470040.
Comment By: Tim Peters (tim_one) Date: 2001-10-04 13:57
Message: Logged In: YES user_id=31435
Looks like I assigned this to Guido by mistake; back to me.
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-470040 ] ParseTuple t# vs subclasses
- Next message: [Python-bugs-list] [ python-Bugs-467384 ] provide a documented serialization func
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]