I have a Json function below need to construct a class having two function, How my second function will "know" that the data
which is response from first function
def results():
json_request = request.get_json()
data = json.loads(json.dumps(json_request))
return Response(data, mimetype='xml/html')
My pseudo-code is below:
class Myjson():
def function_one():
json_request = request.get_json()
data = json.loads(json.dumps(json_request))
def function_two():
return Response(data, mimetype='xml/html')
You have to save your data
as a property of the underlying self
object of your class, e.g.:
class MyJson():
def func1(self):
self.data = ...
def func2(self):
return Response(self.data, ...)
x = MyJson()
x.func1()
y = x.func2()
Note that it is a good programming practice not to introduce new class attributes outside the constructor, so in practice you may want to add to the __init__()
method something to initialize self.data
, e.g.:
class MyJson():
def __init__(self):
self.data = None
def func1(self):
self.data = ...
def func2(self):
return Response(self.data, ...)
EDIT
(To address the request for a more reputable source.)
Essentially, all the elements can be found in the official Python tutorial on the chapter dedicated to classes.
Of particular relevance to this question are:
- the discussion on scopes
- the introduction of the class objects
- the discussion on class and instance variables
- some parts of the random remarks (especially at the end)