Я пишу Python-оболочку веб-API с таким классом, как этот
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)
Как видите, я использую метод _http()
, чтобы упростить взаимодействие с объектом httplib2.Http()
. Этот метод часто вызывается внутри класса, и мне интересно, как лучше взаимодействовать с этим объектом:
- создайте объект в
__init__
и затем повторно используйте его при вызове метода _http()
(как показано в коде выше )
- или создайте объект
httplib2.Http()
внутри метода для каждого вызова метода _http()
(как показано в примере кода ниже )
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)