小编Par*_*jan的帖子

__new__覆盖不起作用

我是否知道为什么myClass1以及myClass2在覆盖__new__()方法方面表现不同?建议哪种方式写一个类,为什么?我想myClass1():甚至不打电话__new__(cls),我是对的吗?

$ python
Python 2.7.5+ (default, Sep 19 2013, 13:49:51) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class myClass1():
...     def __new__(cls):
...             print cls.__name__
... 
>>> class myClass2(object):
...     def __new__(cls):
...             print cls.__name__
... 
>>> o1 = myClass1()
>>> o2 = myClass2()
myClass2
>>> 
Run Code Online (Sandbox Code Playgroud)

python oop python-2.7

1
推荐指数
1
解决办法
53
查看次数

标签 统计

oop ×1

python ×1

python-2.7 ×1