Skip to main content
 首页 » 编程设计

python之尽管不使用输出,但仍需要存储 Python 函数的输出

2024年10月01日8tintown

我试图理解为什么我必须存储 Python 函数的输出(无论我使用的变量名称如何,也无论我随后是否使用该变量)。我认为这对 Python 更通用,而不是专门针对软件 NEURON,因此我将其放在 Stackoverflow 上。

兴趣线在这里:

clamp_output = attach_current_clamp(cell) 

如果我只写 attach_current_clamp(cell),而不将函数的输出存储到变量中,则代码不起作用(绘图为空),但我不使用 clamp_output 完全没有。 为什么我不能直接调用函数?为什么即使不使用输出也必须使用变量来存储输出?

import sys 
import numpy 
sys.path.append('/Applications/NEURON-7.4/nrn/lib/python') 
from neuron import h, gui 
from matplotlib import pyplot 
 
#SET UP CELL 
class SingleCell(object): 
    def __init__(self): 
        self.soma = h.Section(name='soma', cell=self) 
        self.soma.L = self.soma.diam = 12.6517 
        self.all = h.SectionList() 
        self.all.wholetree(sec=self.soma) 
        self.soma.insert('pas') 
        self.soma.e_pas = -65 
 
        for sec in self.all: 
            sec.cm = 20 
#CURRENT CLAMP 
def attach_current_clamp(cell): 
    stim = h.IClamp(cell.soma(1)) 
    stim.delay = 100 
    stim.dur = 300 
    stim.amp = 0.2 
    return stim 
 
cell = SingleCell() 
 
#IF I CALL THIS FUNCTION WITHOUT STORING THE OUTPUT, THEN IT DOES NOT WORK 
clamp_output = attach_current_clamp(cell) 
 
#RECORD AND PLOT 
soma_v_vec = h.Vector() 
t_vec = h.Vector() 
soma_v_vec.record(cell.soma(0.5)._ref_v) 
t_vec.record(h._ref_t) 
h.tstop = 800 
h.run() 
pyplot.figure(figsize=(8,4)) 
soma_plot = pyplot.plot(t_vec,soma_v_vec) 
pyplot.show() 

请您参考如下方法:

这是 NEURON+Python 特定的错误/功能。它与 Python 垃圾收集和 NEURON 实现 Python-HOC 接口(interface)的方式有关。

当 Python 或 HOC 中不再有对 NEURON 对象(例如 IClamp)的引用时,该对象将从 NEURON 中删除。

将 IClamp 保存为单元格的属性可以避免问题,就像保存结果一样,因此这可能是您的一个选择:

# In __init__: 
self.IClamps = [] 
 
# In attach_current_clamp: 
stim.amp = 0.2 
cell.IClamps.append(stim) 
#return stim