python-3.x - 在终端 debian 中运行 python 脚本 (file.py),错误 (<function b at 0x7f63232c01e0>)

标签 python-3.x linux debian-buster

我在终端中运行一个 python 脚本 (file.py),它不打印函数的结果。您可以在下面找到我的终端机的打印件,谢谢。

stest@debian:~/Documents/python$ ls -l | grep tic_tac.py 
-rwxr-xr-x  1 stest stest  270 Oct 14 15:58 tic_tac.py
stest@debian:~/Documents/python$ cat tic_tac.py 

    #!/usr/bin/env python
    d_b = ["  " for i in range(9)]
    print (d_b)
    def b():
        r_1 = "|{}|{}|{}|".format(d_b[0],d_b[1],d_b[2])
        r_2 = "|{}|{}|{}|".format(d_b[3],d_b[4],d_b[5])
        r_3 = "|{}|{}|{}|".format(d_b[6],d_b[7],d_b[8])
        print (r_1)
        print (r_2)
        print (r_3)
    print (b)

stest@debian:~/Documents/python$ ./tic_tac.py 

    ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
    <function b at 0x7f6bd9f28668>

stest@debian:~/Documents/python$ python3 tic_tac.py 

    ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
    <function b at 0x7f63232c01e0>

stest@debian:~/Documents/python$ 

最佳答案

您正在打印函数对象。如果要执行函数,则必须在函数名称后使用括号。由于您的函数没有返回值,因此“print”也会在输出中显示“无”值。所以在这种情况下使用 print 是多余的,您只需要立即调用该函数即可。

def b():   # Your function definition
    ...

b()    # Function call (The correct way)
print(b())    # Executes the function but also prints 'None'
print(b)    # Only prints the function object without executing it

关于python-3.x - 在终端 debian 中运行 python 脚本 (file.py),错误 (<function b at 0x7f63232c01e0>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58377960/

相关文章:

python - 为什么 `if None.__eq__("a")` 似乎评估为 True(但不完全)?

python - 我的名字中有两个字母数量相等,如何打印这两个字母及其数量

linux - 当非阻塞服务器套接字上收到新请求时,为什么进程不从 sleep() 恢复?

linux - 使用分隔符连接多个 yaml 文件

mysql - 无法在 Debian 10 上安装 Mysql 社区服务器

apt-get - E : The repository 'http://ppa.launchpad.net/certbot/certbot/ubuntu focal Release' does not have a Release file

python - 如何为 python 3 和 python 2 运行 pip

python - pycharm 中的“预期语句结束”

c - 如何使用 C 获取 Linux 中的 CPU 数量?

linux - 更新GLEW后,窗口变成黑色