I'm writing a pythonic web API wrapper with a class like this
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'm using the _http()
method to simplify the interaction with the httplib2.Http()
object. This method is called quite often inside the class and I'm wondering what's the best way to interact with this object:
- create the object in the
__init__
and then reuse it when the_http()
method is called (as shown in the code above) - or create the
httplib2.Http()
object inside the method for every call of the_http()
method (as shown in the code sample 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)