相关疑难解决方法(0)

Python print不使用__repr __,__ unicode__或__str__作为unicode子类?

Python打印没有使用__repr__,__unicode____str__打印时我的unicode子类.关于我做错了什么的线索?

这是我的代码:

使用Python 2.5.2(r252:60911,2009年10月13日,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'
Run Code Online (Sandbox Code Playgroud)

我不确定这是否是上述的准确近似值,只是为了比较:

>>> 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 …
Run Code Online (Sandbox Code Playgroud)

python unicode class subclass derived-class

10
推荐指数
2
解决办法
3347
查看次数

标签 统计

class ×1

derived-class ×1

python ×1

subclass ×1

unicode ×1