小编use*_*915的帖子

Python:__str__,但对于一个类,而不是一个实例?

我理解以下Python代码:

>>> class A(object):
...     def __str__(self):
...         return "An instance of the class A"
... 
>>> 
>>> a = A()
>>> print a
An instance of the class A
Run Code Online (Sandbox Code Playgroud)

现在,我想改变输出

>>> print A
<class '__main__.A'>
Run Code Online (Sandbox Code Playgroud)

我需要哪个函数来重载才能做到这一点?即使从未实例化类,解决方案也必须工作.Python 2.x和3中的情况有所不同吗?

python string class

8
推荐指数
2
解决办法
8896
查看次数

标签 统计

class ×1

python ×1

string ×1