从命令行运行功能 [英] Run function from the command line

查看:85
本文介绍了从命令行运行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

def hello():
    return 'Hi :)'

如何直接从命令行运行它?

How would I run this directly from the command line?

推荐答案

使用 -c (命令)自变量(假设您的文件名为 foo.py ):

With the -c (command) argument (assuming your file is named foo.py):

$ python -c 'import foo; print foo.hello()'

或者,如果您不关心名称空间污染,则: p>

Alternatively, if you don't care about namespace pollution:

$ python -c 'from foo import *; print hello()'

中间点:

$ python -c 'from foo import hello; print hello()'

这篇关于从命令行运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆