在 Python 中,在函数名称前使用"if not"是什么意思?



我遇到了一些我无法编写的代码,对此没有任何解释。我试过了!该代码分为两部分,用于运行计算机健康检查。在第二块中,"如果不是"这一行是什么意思?(我不熟悉以这种方式使用函数名(。评论显示了它的作用,但我对它的工作原理很感兴趣。在同一行,括号中斜线的含义是什么?

#!/usr/bin/env python3
import requests
import socket
def check_localhost():
localhost = socket.gethostbyname('localhost')
return localhost == "127.0.0.1"
def check_connectivity():
request = requests.get("http://www.google.com")
return request == 200
#!/usr/bin/env python3
from network import *
import shutil
import psutil
def check_disk_usage(disk):
"""Verifies that there's enough free space on disk"""
du = shutil.disk_usage(disk)
free = du.free / du.total * 100
return free > 20
def check_cpu_usage():
"""Verifies that there's enough unused CPU"""
usage = psutil.cpu_percent(1)
return usage < 75

# If there's not enough disk, or not enough CPU, print an error
if not check_disk_usage('/') or not check_cpu_usage():
print("ERROR!")
elif check_localhost() and check_connectivity():
print("Everything ok")
else:
print("Network checks failed")

Python中的not与其他语言中的!等价;它是一个否定者。如果以下语句不为true,则返回true,否则返回false。

据推测,括号中的/指的是Unix文件系统中的根目录。

所以这句话是说;如果root所在的磁盘空间不足,或者cpu不足,请打印一个错误"两个函数check_disk_usage(folder)check_cpu_usage()返回布尔值true(表示正常(或false(表示不正常(。

最新更新