我想弄清楚这个:
c = 1
def f(n):
print c + n
def g(n):
c = c + n
f(1) # => 2
g(1) # => UnboundLocalError: local variable 'c' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
谢谢!
我的代码如下:
done = False
def function():
for loop:
code
if not comply:
done = True #let's say that the code enters this if-statement
while done == False:
function()
Run Code Online (Sandbox Code Playgroud)
出于某种原因,当我的代码进入if语句时,它在使用function()完成后不会退出while循环.
但是,如果我这样编码:
done = False
while done == False:
for loop:
code
if not comply:
done = True #let's say that the code enters this if-statement
Run Code Online (Sandbox Code Playgroud)
...它退出while循环.这里发生了什么?
我确保我的代码输入if语句.我还没有运行调试器因为我的代码有很多循环(非常大的2D数组)而且我放弃了调试,因为它太繁琐了.为什么"完成"在功能中没有被改变?
我有一个信号处理程序来处理ctrl-c中断.如果在信号处理程序中我想读取我的主脚本中的变量集,那么在设置变量时是否可以使用"global"语句?
我不介意这样做,但阅读这篇文章(你在Python中使用"全局"语句吗?),其中有人评论说没有理由使用全局.
在这种情况下有什么选择?
我的代码看起来像这样:
def signal_handler(signal, frame):
print "in sig handler - g_var=%s" % g_var
def main():
global g_var
g_var = "test"
time.sleep(120)
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
main()
Run Code Online (Sandbox Code Playgroud) 我有一个非常基本的问题.
假设我调用一个函数,例如,
def foo():
x = 'hello world'
Run Code Online (Sandbox Code Playgroud)
如何让函数以这样的方式返回x,我可以将它用作另一个函数的输入或者在程序体内使用变量?
当我使用return并在另一个函数中调用该变量时,我得到一个NameError.
所以我试图通过Python设置多项选择测验.我对Python很陌生,所以如果有更简单的方法可以预先表示道歉.但是,在尝试更新的技术之前,我正试图真正理解一些基础知识.
我有一本字典.在这本字典中,我想要抓住3个随机密钥.我还想确保这三个键不相等(换句话说,彼此随机).这是我到目前为止写的代码:
import random
word_drills = {'class': 'Tell Python to make a new kind of thing.',
'object': 'Two meanings: the most basic kind of thing, and any instance of some thing.',
'instance': 'What you get when you tell Python to create a class.',
'def': 'How you define a function inside a class.',
'self': 'Inside the functions in a class, self is a variable for the instance/object being accessed.',
'inheritance': 'The concept that one class can inherit traits from another class, …Run Code Online (Sandbox Code Playgroud) IPython 如何处理局部变量?我有这个函数在 Python shell 中工作,但在 IPython shell 中不起作用。
def change(key,value):
global aname
global alist
alist.append(key)
aname.extend(value)
Run Code Online (Sandbox Code Playgroud)
我在for循环中使用它,它从 JSON 和其他 .txt 文件中读取输入,并将键和值添加到列表中,然后由另一个函数使用以保存到数据库。如果我不这样做,它将很难看,并且会在我的循环中使用索引。
[change(key,value) for key,value in jsondata.itervalues()]
def storeindatabase():
do_sothing to the list aname and store
do_sothing to the alist and store
Run Code Online (Sandbox Code Playgroud) 我正试图从 Matlab 飞跃到 numpy,但我迫切需要我的 fft 的速度。现在我知道 pyfftw,但我不知道我是否正确使用它。我的方法是这样的
import numpy as np
import pyfftw
import timeit
pyfftw.interfaces.cache.enable()
def wrapper(func, *args):
def wrapped():
return func(*args)
return wrapped
def my_fft(v):
global a
global fft_object
a[:] = v
return fft_object()
def init_cond(X):
return my_fft(2.*np.cosh(X)**(-2))
def init_cond_py(X):
return np.fft.fft(2.*np.cosh(X)**(-2))
K = 2**16
Llx = 10.
KT = 2*K
dx = Llx/np.float64(K)
X = np.arange(-Llx,Llx,dx)
global a
global b
global fft_object
a = pyfftw.n_byte_align_empty(KT, 16, 'complex128')
b = pyfftw.n_byte_align_empty(KT, 16, 'complex128')
fft_object = pyfftw.FFTW(a,b)
wrapped = …Run Code Online (Sandbox Code Playgroud)