Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么python';BaseHTTPServer是旧式类吗?_Python_Python 2.x_Httpserver - Fatal编程技术网

为什么python';BaseHTTPServer是旧式类吗?

为什么python';BaseHTTPServer是旧式类吗?,python,python-2.x,httpserver,Python,Python 2.x,Httpserver,python的BaseHTTPServer.HTTPServer是一个老式类,有什么原因吗 >>> import BaseHTTPServer >>> type(BaseHTTPServer.HTTPServer) classobj 我问这个问题是因为我想在一个继承自HTTPServer的类中使用super,但不能。有一个解决办法: class MyHTTPServer(HTTPServer,object): ... 此解决方案是否有任何隐藏的“陷

python的
BaseHTTPServer.HTTPServer
是一个老式类,有什么原因吗

>>> import BaseHTTPServer
>>> type(BaseHTTPServer.HTTPServer)
classobj
我问这个问题是因为我想在一个继承自HTTPServer的类中使用
super
,但不能。有一个解决办法:

class MyHTTPServer(HTTPServer,object):
    ...
此解决方案是否有任何隐藏的“陷阱”

这个问题在Python3中得到了纠正,在Python3中,所有类都是新样式的类


现在,我们只看到了新样式类的优点,并且我们习惯于以与新样式兼容的方式编程。然而,当经典类成为标准时,可能会有这样的代码:

def __str__():
    return "I'm Classic"

class Classic: pass

c = Classic()
c.__str__ = __str__
print(c)
哪张照片

I'm Classic
但是,如果将经典类更改为新样式,那么这种在实例上定义特殊方法的方法将被打破:

class New(object): pass
n = New()
n.__str__ = __str__
print(n)
印刷品

<__main__.New object at 0xb746ad4c>

对于新样式的类,必须在对象的类(或MRO)中定义特殊方法,如
\uuu str\uu
,以使其影响对象。旧式类的情况并非如此


由于Python2旨在向后兼容,类似这样的差异阻止Python2将标准库中的经典类更改为新样式。

哪个Python版本?@LutzHorn:Python3只有新样式的类,因此2.X.Blender做对了;2.7.2尽管如此。我可能应该更新到2.7.6。fwiw,我在这个模块中使用了
6
,所以它将是py3兼容的,嗯,做得不错。。。您必须具备python列表的百科全书式知识。我很高兴接受这个事实,但如果你有任何关于“gotchas”方面的想法,这将是有益的。
<__main__.New object at 0xb746ad4c>