我创建了一个简单的函数,通过在eval()函数中传递变量名,在一些工程脚本中报告变量的当前值。该参数作为字符串传递,然后eval()读取该参数并用一些附加信息报告该值。该函数在单个脚本中正常工作。但是,当我从一个模块导入相同的函数时,我返回一个错误,表示变量has未定义
我尝试将其设置为全局变量,但仍然遇到相同的问题
def report(name,units = '-',comment ='NC'):
if type(eval(name)) == str:
print('{0:<12}= {1:^10} {2:^5} {3}'.format(name,eval(name),units,comment))
else:
print('{0:<12}= {1:8.3f} {2:^5} {3}'.format(name,eval(name),units,comment))
在尝试使用导入模块中的函数时,我得到以下信息
>>>from reporting import*
>>> from shapes import*
>>> Iyy = rec_Iyy(40,60)
>>> report('Iyy')
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
File "C:\Users\vousvoukisi\OneDrive\11.Python\03_myScripts\design_mod\reporting.py", line 8, in report
if type(eval(name)) == str:
File "<string>", line 1, in <module>
NameError: name 'Iyy' is not defined
## while i would expect the outcome to be :
>>> %Run reporting.py
Iyy = 720000.000 - NC
目前没有回答
相关问题 更多 >
编程相关推荐