让 super() 在 Python 的 urllib2.Request 中工作
今天下午,我花了几个小时试图在 urllib2.Request
的自定义扩展中找到错误。正如我发现的那样,问题是 super(ExtendedRequest, self)
的使用,因为 urllib2.Request
(我使用的是 Python 2.5)仍然是旧样式类,其中不可能使用 super()
。
创建具有这两种功能的新类的最明显方法
class ExtendedRequest(object, urllib2.Request):
def __init__():
super(ExtendedRequest, self).__init__(...)
是行不通的。调用它,我留下了由 urllib2.Request.__getattr__()
引发的 AttributeError: type
。现在,在我开始复制并粘贴 /usr/lib/python 中的整个 urllib2.Request
类之前,只是为了像
class Request(object):
任何人的想法一样重写它,我如何以更优雅的方式实现这一点? (这个urllib2.Request
的新式类,并支持super()
.)
编辑:顺便说一句:AttributeError 提到:
>>> class ExtendedRequest(object, urllib2.Request):
... def __init__(self):
... super(ExtendedRequest, self).__init__('http://stackoverflow.com')
...
>>> ABC = ExtendedRequest ()
>>> d = urllib2.urlopen(ABC)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/urllib2.py", line 124, in urlopen
return _opener.open(url, data)
File "/usr/lib/python2.5/urllib2.py", line 373, in open
protocol = req.get_type()
File "/usr/lib/python2.5/urllib2.py", line 241, in get_type
if self.type is None:
File "/usr/lib/python2.5/urllib2.py", line 218, in __getattr__
raise AttributeError, attr
AttributeError: type
This afternoon I spent several hours trying to find a bug in my custom extension to urllib2.Request
. The problem was, as I found out, the usage of super(ExtendedRequest, self)
, since urllib2.Request
is (I'm on Python 2.5) still an old style class, where the use of super()
is not possible.
The most obvious way to create a new class with both features,
class ExtendedRequest(object, urllib2.Request):
def __init__():
super(ExtendedRequest, self).__init__(...)
doesn't work. Calling it, I'm left with AttributeError: type
raised by urllib2.Request.__getattr__()
. Now, before I start and copy'n paste the whole urllib2.Request
class from /usr/lib/python just to rewrite it as
class Request(object):
has anyone an idea, how I could achieve this in a more elegant way? (With this being to have a new-style class based on urllib2.Request
with working support for super()
.)
Edit: By the way: the AttributeError mentioned:
>>> class ExtendedRequest(object, urllib2.Request):
... def __init__(self):
... super(ExtendedRequest, self).__init__('http://stackoverflow.com')
...
>>> ABC = ExtendedRequest ()
>>> d = urllib2.urlopen(ABC)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/urllib2.py", line 124, in urlopen
return _opener.open(url, data)
File "/usr/lib/python2.5/urllib2.py", line 373, in open
protocol = req.get_type()
File "/usr/lib/python2.5/urllib2.py", line 241, in get_type
if self.type is None:
File "/usr/lib/python2.5/urllib2.py", line 218, in __getattr__
raise AttributeError, attr
AttributeError: type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该可以正常工作,因为层次结构很简单
This should work fine since the hierarchy is simple
使用 super 可能并不总是最佳实践。使用super有很多困难。请阅读 James Knight 的 http://fuhm.org/super-harmful/ 的示例。
该链接显示(除其他问题外)
__init__
必须准备好调用层次结构中任何其他类的__init__
方法。在您的情况下,上述每个标准都被违反。
詹姆斯·奈特还说,
正确使用 super 的条件非常繁琐,我认为 super 的用处相当有限。与子类化相比,更喜欢组合设计模式。如果可以的话,避免钻石继承。如果您从上(对象)到下控制对象层次结构,并一致地使用 super,那么就可以了。但由于在这种情况下您无法控制整个类层次结构,因此我建议您放弃使用
super
。Using super may not always be the best-practice. There are many difficulties with using super. Read James Knight's http://fuhm.org/super-harmful/ for examples.
That link shows (among other issues) that
__init__
signatures of all subclasses that use super should match. You must pass all arguments you receive on to the super function. Your__init__
must be prepared to call any other class's__init__
method in the hierarchy.__init__
In your situation, each of the above critera is violated.
James Knight also says,
The conditions under which super can be used correctly are sufficiently onerous, that I think super's usefulness is rather limited. Prefer the Composition design pattern over subclassing. Avoid diamond inheritance if you can. If you control the object hierarchy from top (object) to bottom, and use super consistently, then you are okay. But since you don't control the entire class hierarchy in this case, I'd suggest you abandon using
super
.我认为您错过了将 self 参数传递给示例中 init 的定义。
试试这个:
我测试了它,它似乎工作正常:
I think you missed to pass the self parameter to definition of init in your sample.
Try this one:
I tested it and it seems to work okey: