问题是,
__str__的
unipre子类。
来自
PyFile_WriteObject,由
intPyFile_WriteObject(PyObject *v, PyObject *f, int flags){... if ((flags & Py_PRINT_RAW) && PyUnipre_Check(v) && enc != Py_None) { char *cenc = PyString_AS_STRING(enc); char *errors = fobj->f_errors == Py_None ? "strict" : PyString_AS_STRING(fobj->f_errors); value = PyUnipre_AsEnpredString(v, cenc, errors); if (value == NULL) return -1;PyUnipre_Check(v)如果
v类型为
unipre或子类, 则返回true
。因此,该代码无需咨询即可直接编写unipre对象
__str__。
请注意,子类化
str和重写
__str__按预期工作:
>>> class mystr(str):... def __str__(self): return "str"... def __repr__(self): return "repr"... >>> print mystr()str
像调用
str或
unipre显式一样:
>>> class myuni(unipre):... def __str__(self): return "str"... def __repr__(self): return "repr"... def __unipre__(self): return "unipre"... >>> print myuni()>>> str(myuni())'str'>>> unipre(myuni())u'unipre'
我相信这可以解释为目前实现的Python错误。



