This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2012年10月08日 16:58 by Radu.Dan, last changed 2022年04月11日 14:57 by admin. This issue is now closed.
| Messages (4) | |||
|---|---|---|---|
| msg172396 - (view) | Author: Radu Dan (Radu.Dan) | Date: 2012年10月08日 16:58 | |
Classes that extend the builtin 'str' cannot be printed as is, and are automatically converted to string.
#!/bin/env python
class newstring(str):
def __str__(self):
return self
a = newstring("hello world")
print a
Running this returns:
Traceback (most recent call last):
File "./test.py", line 7, in <module>
print a
RuntimeError: print recursion
Given that instances of 'str' are immutable, I see no reason why this should not work.
|
|||
| msg172407 - (view) | Author: Benjamin Peterson (benjamin.peterson) * (Python committer) | Date: 2012年10月08日 20:01 | |
Python 3.4.0a0 (default:8f048c8c855e, Oct 8 2012, 13:46:48) [GCC 4.5.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> class newstring(str): ... def __str__(self): ... return self ... >>> type(str(newstring())) <class '__main__.newstring'> |
|||
| msg172408 - (view) | Author: Benjamin Peterson (benjamin.peterson) * (Python committer) | Date: 2012年10月08日 20:02 | |
And indeed >>> print(a) hello world |
|||
| msg172409 - (view) | Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) | Date: 2012年10月08日 20:15 | |
Radu Dan: Printing of str-derived classes works in Python >=3.0, so you should just upgrade to newer version of Python. New features will not be backported to Python 2. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:37 | admin | set | github: 60371 |
| 2012年10月08日 20:15:21 | Arfrever | set | resolution: not a bug -> works for me messages: + msg172409 nosy: + Arfrever |
| 2012年10月08日 20:02:55 | benjamin.peterson | set | messages: + msg172408 |
| 2012年10月08日 20:01:22 | benjamin.peterson | set | status: open -> closed nosy: + benjamin.peterson messages: + msg172407 resolution: not a bug |
| 2012年10月08日 16:58:20 | Radu.Dan | create | |