简单实例:
class TmpTest: def __init__(self,x,y): self.x = x self.y = y def __call__(self,y): self.x,self.y = x,ya = TmpTest(1,2)a(4,5)print(a.x,a.y)4 5
实战中应用:
import Jsonimport requestsfrom common.RecordLog import logclass httpRequests(object): def __init__(self): self.session = requests.Session() log.info(‘建立请求...‘) def send_request(self,method,url,params_type=‘form‘,data=None,**kwargs): method = method.upper() params_type = params_type.upper() if isinstance(data,str): try: data = Json.loads(data) except Exception: data = eval(data) if ‘GET‘ == method: response = self.session.request(method=method,url=url,params=data,**kwargs) elif ‘POST‘ == method: if params_type == ‘FORM‘: log.info("开始发送{}请求,URL为:{},请求数据为:{}".format(method,data)) response = self.session.request(method=method,data=data,**kwargs) elif params_type == ‘JsON‘: response = self.session.request(method=method,Json=data,**kwargs) else: response = self.session.request(method=method,**kwargs) else: log.error("请求方法错误:request method ‘{}‘ error ! please check".format(method)) raise ValueError(‘request method "{}" error ! please check‘.format(method)) return response def __call__(self,**kwargs): return self.send_request(method,params_type=params_type,data=data,**kwargs) def close_session(self): self.session.close() try: log.info(‘关闭请求...‘) del self.session.cookies[‘JsESSIONID‘] except Exception: passrequest = httpRequests()总结
以上是内存溢出为你收集整理的python中的__call__函数全部内容,希望文章能够帮你解决python中的__call__函数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)