【问题标题】:Running a Python function in BASH在 BASH 中运行 Python 函数
【发布时间】:2022-11-30 16:27:19
【问题描述】:

我通常在 Google Colab 上运行 Python,但是我需要在 Ubuntu 的终端中运行一个脚本。

我有以下脚本 测试.py:

#!/usr/bin/env python

# testing a func

def hello(x):
  if x > 5:
    return "good"
  else:
    return "bad"

hello(2)

执行时它没有返回任何东西。现在我可以用 print 语句替换 return 语句。但是,对于我拥有的其他脚本,需要返回语句。

我试过了:

python test.py

你看,在 Google Colab 上,我可以简单地调用函数 (hello(2)),它就会执行。

期望的输出:

> python test.py
> bad
 

【问题讨论】:

    标签: python bash ubuntu terminal


    【解决方案1】:

    您不会向STDOUT 打印任何内容,因此您不会在终端中看到good/bad

    您应该在代码中将 hello(2) 行更改为 print(hello(2))(在这种情况下,hello(2) 函数调用的返回值将打印到 STDOUT 文件描述符),然后您将在终端中看到结果。

    【讨论】:

      猜你喜欢
      • 2020-08-11
      • 2015-06-26
      • 2011-04-13
      • 2015-12-18
      • 1970-01-01
      • 2011-05-14
      • 2018-04-22
      • 2014-06-15
      相关资源
      最近更新 更多