Python 子类化关键字参数
我的应用程序中的Flask子类化有问题。我的类init中出现意外的关键字参数异常 app/controller.pyPython 子类化关键字参数,python,Python,我的应用程序中的Flask子类化有问题。我的类init中出现意外的关键字参数异常 app/controller.py from app.searchapi import SearchService [...] def main(args, config): app = SearchService(someValue=True) app.run(threaded=True, use_reloader=False, debug=False, host='127
from app.searchapi import SearchService
[...]
def main(args, config):
app = SearchService(someValue=True)
app.run(threaded=True, use_reloader=False, debug=False,
host='127.0.0.1', port=5000)
app/searchapi.py
from flask import Flask, jsonify, request, make_response, json
class SearchService(Flask):
def __init__(self, *args, **kwargs):
if not args:
kwargs.setdefault('import_name',__name__)
self.someValue = kwargs.get('someValue')
super(SearchService, self).__init__(*args, **kwargs)
self.route("/", methods=['GET'])(self.HelloWorld)
def HelloWorld(self):
return "Hello, World"
返回
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/Users/div/Project/app/controller.py", line 158, in main
app = SearchService(someValue=True)
File "/Users/div/Project/app/searchapi.py", line 15, in __init__
super(SearchService, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'someValue'
您正在将
someValue
kwarg传递给超类Flask,这是意外的。尝试以下方法,而不是get
ting它:
self.someValue = kwargs.pop('someValue')
这会将其从kwargs中移除,当您将其传递到烧瓶时,它就消失了