2016-01-26 77 views
0

在下面的代码中,我有两个函数:func1gillespie。我已将p_tot_stoch定义为全局变量,在func1之内。 (将它放入函数的原因是为了允许Numba的@jit包装正常工作... Numba用于代码优化。)如何在Python中的多个函数之间共享变量,即全局?

但是,当我尝试在代码的最后打印p_tot_stoch时,我得到以下错误信息:

Traceback (most recent call last): 
    File "C:/Users/dis_YO_boi/Documents/Programming/Python/CodeReview.py", line 85, in <module> 
    p_tot_stoch = gillespie() 
NameError: global name 'p_tot_stoch' is not defined 

我宣布它为全球性的,但它看起来像的主要功能gillespie不能访问它。我怎样才能解决这个问题?

我的代码如下,谢谢你的帮助。

from __future__ import division 
import numpy as np 
import matplotlib.pyplot as plt 
from numba import jit 
import math 
random = np.random 

DELTA = 1e-3 
SIM_NUM = 100 
K_MINUS = 1e-3 
K_CAT = 1 - 1e-3 
ZETA = 1e-4 
D = 10000 
QP_VEC = np.logspace(-2, 1, 101, 10) 
KAPPA_M = (K_CAT + K_MINUS)/ZETA 
P0_VEC = QP_VEC/DELTA 
QP_DEG = np.true_divide(1000*D*QP_VEC*K_CAT,1000*QP_VEC+KAPPA_M) 

@jit 
def func1(): 
    global p_tot_stoch 
    p_tot_stoch = np.empty((SIM_NUM, len(QP_VEC))) 

@jit 
def gillespie(max_time=1000): 
    for len_qp_ind in range(len(QP_VEC)): 
     qp = QP_VEC[len_qp_ind] 
     p0 = math.ceil(P0_VEC[len_qp_ind]) 
     for sim_num_ind in range(SIM_NUM): 
      p = p0 
      d = D 
      dp = time = 0 

      while True: 
       tot = [qp, ZETA * p * d, K_MINUS * dp, K_CAT * dp] 
       for i in range(3): 
        tot[i + 1] += tot[i] 
       p_tot = p + dp 
       kt = tot[-1] 
       time += -np.log(1 - random.random())/kt 
       if time > max_time: 
        p_tot_stoch[sim_num_ind, len_qp_ind] = p_tot 
        break 

       event_kt = random.random() * kt 
       if event_kt < tot[0]: 
        p += 1 
       elif event_kt < tot[1]: 
        p -= 1 
        dp += 1 
        d -= 1 
       elif event_kt < tot[2]: 
        p += 1 
        dp -= 1 
        d += 1 
       elif event_kt < tot[3]: 
        dp -= 1 
        d += 1 
    return p_tot_stoch 

if __name__ == '__main__': 
     p_tot_stoch = gillespie() 
     p_mean = p_tot_stoch.mean(axis=0) 
     p_std = p_tot_stoch.std(axis=0) 
     print(p_tot_stoch) 

回答

0

猫test.py

my_variable = 0 


def func1(): 
    global my_variable 
    my_variable = -1 
    print "func1:{0}".format(my_variable) 


def gillespie(): 
    global my_variable 
    my_variable = 4 
    print "gillespie:{0}".format(my_variable) 


# Starts testing... 
print "before:{0}".format(my_variable) 
func1() 
gillespie() 
print "after:{0}".format(my_variable) 

蟒蛇test.py

before:0 
func1:-1 
gillespie:4 
after:4 

你可以声明你的变量p_tot_stoch(在我的test.py我宣布了一个名为my_varialble变量它是用来在func1()gillespie())位于脚本的顶部和函数之外。每次你想修改它时,你都必须声明它是一个global变量,然后给它赋一个新的值。

即时通讯使用python2.7

0

我修改@ haifzhan的例子,因为它很简单。 Python极大地受益于OOP,并且不使用它是一种罪恶:

#!/usr/bin/env python3 

class Stoch: 
    def __init__(self): 
     self.my_variable = 0 

    def __str__(self): 
     return str(self.my_variable) 

    def func1(self): 
     self.my_variable = -1 
     print ("func1:", self) 

    def gillespie(self): 
     self.my_variable = 4 
     print ("gillespie:", self) 

    @classmethod 
    def main(cls): 
     stoch = Stoch() 
     print ("before:", stoch)  
     stoch.func1() 
     stoch.gillespie() 
     print ("after:", stoch) 

if __name__ == '__main__': 
    Stoch.main() 
相关问题