有没有办法检查函数输出是否分配给Python中的变量?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了有没有办法检查函数输出是否分配给Python中的变量?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2933字,纯文字阅读大概需要5分钟。
内容图文
![有没有办法检查函数输出是否分配给Python中的变量?](/upload/InfoBanner/zyjiaocheng/691/c918c3987eed4c34aee03d4a37dc5ae7.jpg)
在Python中,我想编写一个将其结果漂亮地打印到控制台的函数,如果它本身被调用(主要用于交互使用或调试).出于这个问题的目的,我们假设它检查事物的状态.如果我打电话给
check_status()
我希望看到类似的东西:
Pretty printer status check 0.02v
NOTE: This is so totally not written for giant robots
=================================
System operational: ... ok
Time to ion canon charge is 9m 21s
Booster rocket in AFTERBURNER state
Range check is optimal
Rocket fuel is 10h 19m 40s to depletion
Beer served is type WICKSE LAGER, chill optimal
Suggested catchphrase is 01_FIGHTING_SPIRIT_GOGOGO
Virtual ... on
但是,如果我在变量赋值的上下文中调用输出,我也希望它将输出作为列表传递:
not_robot_stat = check_status()
print not_robot_stat
>>> {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5, 'range_est_sigma': 0.023, 'fuel_est': 32557154, 'beer_type': 31007, 'beer_temp': 2, 'catchphrase_suggestion': 1023, 'virtual_on': 'hell yes'}
那么…有没有一种方法可以动态地知道一个函数中的输出是否已分配?我希望能够在不诉诸参数传递或编写另一个专用于此的函数的情况下做到这一点.我已经用Google搜索了一下,从一点也看不出来,看来我不得不诉诸字节码了.那真的有必要吗?
解决方法:
新解决方案
这是一个新的解决方案,它通过检查其自身的字节码来检测何时将函数的结果用于赋值.没有字节码的编写,它甚至应该与将来的Python版本兼容,因为它使用opcode模块进行定义.
import inspect, dis, opcode
def check_status():
try:
frame = inspect.currentframe().f_back
next_opcode = opcode.opname[ord(frame.f_code.co_code[frame.f_lasti+3])]
if next_opcode == "POP_TOP":
# or next_opcode == "RETURN_VALUE":
# include the above line in the if statement if you consider "return check_status()" to be assignment
print "I was not assigned"
print "Pretty printer status check 0.02v"
print "NOTE: This is so totally not written for giant robots"
return
finally:
del frame
# do normal routine
info = {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5}
return info
# no assignment
def test1():
check_status()
# assignment
def test2():
a = check_status()
# could be assignment (check above for options)
def test3():
return check_status()
# assignment
def test4():
a = []
a.append(check_status())
return a
解决方案1
这是旧的解决方案,该解决方案在使用python -i或PDB进行调试时在调用函数时进行检测.
import inspect
def check_status():
frame = inspect.currentframe()
try:
if frame.f_back.f_code.co_name == "<module>" and frame.f_back.f_code.co_filename == "<stdin>":
print "Pretty printer status check 0.02v"
print "NOTE: This is so totally not written for giant robots"
finally:
del frame
# do regular stuff
return {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5}
def test():
check_status()
>>> check_status()
Pretty printer status check 0.02v
NOTE: This is so totally not written for giant robots
{'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5}
>>> a=check_status()
Pretty printer status check 0.02v
NOTE: This is so totally not written for giant robots
>>> a
{'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5}
test()
>>>
内容总结
以上是互联网集市为您收集整理的有没有办法检查函数输出是否分配给Python中的变量?全部内容,希望文章能够帮你解决有没有办法检查函数输出是否分配给Python中的变量?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。