Making super() work in Python's urllib2.Request
-
20-09-2019 - |
Question
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
Solution
This should work fine since the hierarchy is simple
class ExtendedRequest(urllib2.Request):
def __init__(self,...):
urllib2.Request.__init__(self,...)
OTHER TIPS
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
- Superclasses must use super if their subclasses do
- The
__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. - Never use positional arguments in
__init__
In your situation, each of the above critera is violated.
James Knight also says,
The only situation in which super() can actually be helpful is when you have diamond inheritance. And even then, it is often not as helpful as you might have thought.
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
.
I think you missed to pass the self parameter to definition of init in your sample. Try this one:
class ExtendedRequest(object, urllib2.Request):
def __init__(self):
super(ExtendedRequest, self).__init__(self)
I tested it and it seems to work okey:
>>> x = ExtendedRequest()
>>> super(ExtendedRequest, x)
<super: <class 'ExtendedRequest'>, <ExtendedRequest object>>