Python打印不使用__repr__,__unicode__或__str__作为unicode子类? [英] Python print isn't using __repr__, __unicode__ or __str__ for unicode subclass?

查看:250
本文介绍了Python打印不使用__repr__,__unicode__或__str__作为unicode子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python打印不使用 __ repr __ __ unicode __ __ str __ 为我的unicode子类打印时。



这是我的代码:



使用Python 2.5.2 (r252:60911,Oct 13 2009,14:11:59)

 >> class MyUni(unicode):
... def __repr __(self):
... return__repr__
... def __unicode __(self):
... return unicode(__ unicode__)
... def __str __(self):
... return str(__ str__)
...
>> ; s = MyUni(HI)
>>>> s
'__repr__'
>>>> print s
'HI'

我不确定这是否是一个准确的近似以上,但只是为了比较:

 >> class MyUni(object):
... def __new __(cls,s):
... return super(MyUni,cls).__ new __(cls)
... def __repr __ self):
... return__repr__
... def __unicode __(self):
... return unicode(__ unicode__)
... def __str__ (self):
... return str(__ str__)
...
>>> s = MyUni(HI)
>>>> s
'__repr__'
>>>> print s
'__str__'

像最好的方式来获取一个字符串对象isinstance(实例,basestring)并提供对unicode返回值的控制,并与unicode repr是...

 >>> class UserUnicode(str):
... def __repr __(self):
... returnu'%s'%super(UserUnicode,self).__ str __()
。 .. def __str __(self):
... return super(UserUnicode,self).__ str __()
... def __unicode __(self):
... return unicode (UserUnicode,self).__ str __())
...
>>> s = UserUnicode(HI)
>>>> s
u'HI'
>>>> print s
'HI'
>>>> len(s)
2

_ str _ _ repr _ 不会对此示例添加任何内容,但是其想法是显式地显示模式,根据需要进行扩展。



只是为了证明这种模式可以控制:

 >> class UserUnicode(str):
... def __repr __(self):
... returnu'%s'%__repr__
... def __str __ :
... return__str__
... def __unicode __(self):
... return unicode(__ unicode__)
...
>>>> s = UserUnicode(HI)
>>>> s
u'__ repr__'
>>>> print s
'__str__'

想法?

print 不尊重 __ str __ unicode 子类。



PyFile_WriteObject print



  int 
PyFile_WriteObject(PyObject * v,PyObject * f,int flags)
{
。 ..
if((flags& Py_PRINT_RAW)&&
PyUnicode_Check(v)&& enc!= Py_None){
char * cenc = PyString_AS_STRING
char * errors = fobj-> f_errors == Py_None?
strict:PyString_AS_STRING(fobj-> f_errors);
value = PyUnicode_AsEncodedString(v,cenc,errors);
if(value == NULL)
return -1;

PyUnicode_Check(v) code> v 的类型是 unicode 或子类。因此,此代码直接写入unicode对象,而无需咨询 __ str __



请注意,子类化 str 并覆盖 __ str __ 按预期工作:

 >>>> class mystr(str):
... def __str __(self):returnstr
... def __repr __(self):returnrepr
... $ b b>>>打印mystr()
str



unicode 显式:

 > > class myuni(unicode):
... def __str __(self):returnstr
... def __repr __(self):returnrepr
... def __unicode __ self):returnunicode
...
>>> print myuni()

>>>> str(myuni())
'str'
>>> unicode(myuni())
u'unicode'

我相信这可以解释为当前实现的Python中的错误。


Python print isn't using __repr__, __unicode__ or __str__ for my unicode subclass when printing. Any clues as to what I am doing wrong?

Here is my code:

Using Python 2.5.2 (r252:60911, Oct 13 2009, 14:11:59)

>>> class MyUni(unicode):
...     def __repr__(self):
...         return "__repr__"
...     def __unicode__(self):
...         return unicode("__unicode__")
...     def __str__(self):
...         return str("__str__")
...      
>>> s = MyUni("HI")
>>> s
'__repr__'
>>> print s
'HI'

I'm not sure if this is an accurate approximation of the above, but just for comparison:

>>> class MyUni(object):
...     def __new__(cls, s):
...         return super(MyUni, cls).__new__(cls)
...     def __repr__(self):
...         return "__repr__"
...     def __unicode__(self):
...         return unicode("__unicode__")
...     def __str__(self):
...         return str("__str__")
...
>>> s = MyUni("HI")
>>> s
'__repr__'
>>> print s
'__str__'

[EDITED...] It sounds like the best way to get a string object that isinstance(instance, basestring) and offers control over unicode return values, and with a unicode repr is...

>>> class UserUnicode(str):
...     def __repr__(self):
...         return "u'%s'" % super(UserUnicode, self).__str__()
...     def __str__(self):
...         return super(UserUnicode, self).__str__()
...     def __unicode__(self):
...         return unicode(super(UserUnicode, self).__str__())
...
>>> s = UserUnicode("HI")
>>> s
u'HI'
>>> print s
'HI'
>>> len(s)
2

The _str_ and _repr_ above add nothing to this example but the idea is to show a pattern explicitly, to be extended as needed.

Just to prove that this pattern grants control:

>>> class UserUnicode(str):
...     def __repr__(self):
...         return "u'%s'" % "__repr__"
...     def __str__(self):
...         return "__str__"
...     def __unicode__(self):
...         return unicode("__unicode__")
... 
>>> s = UserUnicode("HI")
>>> s
u'__repr__'
>>> print s
'__str__'

Thoughts?

解决方案

The problem is that print doesn't respect __str__ on unicode subclasses.

From PyFile_WriteObject, used by print:

int
PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
{
...
        if ((flags & Py_PRINT_RAW) &&
    PyUnicode_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 = PyUnicode_AsEncodedString(v, cenc, errors);
    if (value == NULL)
        return -1;

PyUnicode_Check(v) returns true if v's type is unicode or a subclass. This code therefore writes unicode objects directly, without consulting __str__.

Note that subclassing str and overriding __str__ works as expected:

>>> class mystr(str):
...     def __str__(self): return "str"
...     def __repr__(self): return "repr"
... 
>>> print mystr()
str

as does calling str or unicode explicitly:

>>> class myuni(unicode):
...     def __str__(self): return "str"
...     def __repr__(self): return "repr"
...     def __unicode__(self): return "unicode"
... 
>>> print myuni()

>>> str(myuni())
'str'
>>> unicode(myuni())
u'unicode'

I believe this could be construed as a bug in Python as currently implemented.

这篇关于Python打印不使用__repr__,__unicode__或__str__作为unicode子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆