I am writing a pythonic web API wrapper with such a class
import httplib2 import urllib class apiWrapper: def __init__(self): self.http = httplib2.Http() def _http(self, url, method, dict): ''' Im using this wrapper arround the http object all the time inside the class ''' params = urllib.urlencode(dict) response, content = self.http.request(url,params,method)
as you can see, I am using the _http() method to simplify the interaction with the httplib2.Http() object. This method is often found inside a class, and I wonder how best to interact with this object:
- create an object in
__init__ and then reuse when the _http() method is called (as shown in the code above ) - or create an
httplib2.Http() object inside the method for each call to the _http() method (as shown in the code example below )
import httplib2 import urllib class apiWrapper: def __init__(self): def _http(self, url, method, dict): '''Im using this wrapper arround the http object all the time inside the class''' http = httplib2.Http() params = urllib.urlencode(dict) response, content = http.request(url,params,method)
tomaz
source share