2016-01-26 108 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() 
相關問題