我正在尝试对一些python构建脚本进行脚本重用。我的“可重用”部件的简化版本如下(_生成.py)地址:
Sources = []
Sources += glob('*.c')
Sources += glob('../FreeRTOS/*.c')
...
def addSources(directory, *rest):
for each in rest:
Sources += ['../'+directory+'/'+each+'.c']
def addSharedSources(*args):
addSources('Shared', *args)
在定制部分,我有(生成.py)地址:
#!/usr/bin/env python
from _build import *
...
#Additional source files from ../Shared, FreeRTOS and *.c are already in
addSharedSources('ccpwrite', 'discovery', 'radioToo', 'telemetry', 'utility')
不幸的是,当我想跑的时候生成.py,我得到一个回溯,看起来像:
Traceback (most recent call last):
File "./build.py", line 8, in <module>
addSharedSources('ccpwrite', 'discovery', 'radioToo', 'telemetry', 'utility')
File "/Users/travisg/Projects/treetoo/Twig/_build.py", line 49, in addSharedSources
addSources('Shared', *args)
File "/Users/travisg/Projects/treetoo/Twig/_build.py", line 46, in addSources
Sources += ['../'+directory+'/'+each+'.c']
UnboundLocalError: local variable 'Sources' referenced before assignment
所以即使我做了通配符导入,当调用import函数时,它似乎并没有引用从原始函数导入的“全局”变量。有办法让它工作吗?我玩弄global
,但那似乎没有达到我想要的效果。你知道吗
这与进口无关。如果直接运行
_build.py
,也会遇到同样的问题。问题是函数addSources
在修改全局Sources
时没有声明它是全局的。在addSources
函数中插入一个global
声明,一切正常。你知道吗说明:这种代码很容易写错。因此python允许您读取一个全局变量而不声明它为全局变量,但不允许修改它。你知道吗
相关问题 更多 >
编程相关推荐