如果我在def get_context_data(self, **kwargs):
coindex_btc_price = requests.get("http://api.coindesk.com/v1/bpi/currentprice.json")
def get_queryset(self, **kwargs):
coindex_btc_price = requests.get("http://api.coindesk.com/v1/bpi/currentprice.json")
函数中设置了变量,有没有办法在'use strict';
var _module;
if (true) {
_module = 'lixo';
} else {
_module = 'express';
}
var A = require('' + _module);
函数中访问该变量?
import numpy as np
from numpy import pi
from bokeh.client import push_session
from bokeh.driving import cosine
from bokeh.plotting import figure, curdoc
x = np.linspace(0, 4*pi, 80)
y = np.sin(x)
p = figure()
r1 = p.line([0, 4*pi], [-1, 1], color="firebrick")
r2 = p.line(x, y, color="navy", line_width=4)
# open a session to keep our local document in sync with server
session = push_session(curdoc())
@cosine(w=0.03)
def update(step):
# updating a single column of the the *same length* is OK
r2.data_source.data["y"] = y * step
r2.glyph.line_alpha = 1 - 0.8 * abs(step)
curdoc().add_periodic_callback(update, 50)
session.show(p) # open the document in a browser
session.loop_until_closed() # run forever
我不想这样设置两次。
答案 0 :(得分:3)
我会重构并创建一个保存值的实例变量:
def __init__(self):
super(ClassName, self).__init__()
self.coindex_btc_price = None
def get_coindex_price(self):
if self.coindex_btc_price is None:
self.coindex_btc_price = coindex_btc_price = requests.get("http://api.coindesk.com/v1/bpi/currentprice.json")
return self.coindex_btc_price
def get_context_data(self, **kwargs):
coindex_btc_price = self.get_coindex_price()
def get_queryset(self, **kwargs):
coindex_btc_price = self.get_coindex_price()