Author jaraco
Recipients jaraco
Date 2013-09-08.16:41:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1378658488.73.0.640329823015.issue18978@psf.upfronthosting.co.za>
In-reply-to
Content
In Python 2.x and 3.2, I used to use a Request subclass I created for overriding the method used:

class MethodRequest(request.Request):
	def __init__(self, *args, **kwargs):
		"""
		Construct a MethodRequest. Usage is the same as for
		`urllib.request.Request` except it also takes an optional `method`
		keyword argument. If supplied, `method` will be used instead of
		the default.
		"""
		if 'method' in kwargs:
			self.method = kwargs.pop('method')
		return request.Request.__init__(self, *args, **kwargs)

	def get_method(self):
		return getattr(self, 'method', request.Request.get_method(self))

In Python 3.3, which now supports a method parameter, it broke this paradigm (because the method is stored in the instance and is always set to None in __init__ if not specified).

I believe a paradigm where the method is stored as a class attribute and possibly overridden in an instance would be much better, allowing for subclasses to simply and directly override the method. For example:

class HeadRequest(MethodRequest):
	method = 'HEAD'

That straightforward example works very well if method is allowed to be a class attribute, but won't work at all if 'method' is always set as an instance attribute in __init__.

And while it's possible for HeadRequest to override __init__, that requires HeadRequest to override that entire signature, which is less elegant than simply setting a class attribute.

For Python 3.4, I'd like to adapt the Request class to allow the Method to be defined at the class level (while still honoring customization at the instance level).
History
Date User Action Args
2013-09-08 16:41:28jaracosetrecipients: + jaraco
2013-09-08 16:41:28jaracosetmessageid: <1378658488.73.0.640329823015.issue18978@psf.upfronthosting.co.za>
2013-09-08 16:41:28jaracolinkissue18978 messages
2013-09-08 16:41:28jaracocreate