我正在尝试为一些 python 构建脚本做一些脚本重用。我的“可重用”部分的缩写版本如下所示(_build.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)
然后在自定义部分,我有类似(build.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')
不幸的是,当我尝试运行 build.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
因此,即使我进行了通配符导入,在调用导入函数时,它似乎并没有引用我从原始导入的“全局”变量。有没有办法让它工作?我玩弄了global
,但这似乎并没有达到我想要的效果。