我正在编写一个类似于此类的
pythonic Web 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()方法时重用它(如上面的代码所示)
>或者为每次调用_http()方法在方法内创建httplib2.Http()对象(如下面的代码示例所示)
- import httplib2
- import urllib
- class apiWrapper:
- def __init__(self):
- def _http(self,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,method)