Python 布尔方法命名约定

Python boolean methods naming convention

我是一名 Ruby 学习者 Python,我想知道 Python 中是否有如下约定。

在 Ruby 中,return 布尔值的方法应该总是以 ? 结尾。例如,

def palindrome?(string)
  # some code that tests whether string is a palindrome
end

我能找到的唯一 existing SO question 没有提供明确的答案。

你可以这样定义

def is_palindrome(variable):
    # your logic\
    # return True / False

没有特定于返回布尔值的方法的标准命名约定。但是,PEP8 确实有 a guide 用于命名功能。

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

通常,人们以 is(例如 is_palindrome)开始一个函数来描述返回一个布尔值。

我同意 on the principle of turning to PEP8,Python 风格指南,作为指导。然而,我会建议 "as necessary to improve readability" 是旁观者的眼睛。例如,这些函数中的每一个都在 Python 中用作 str 对象的函数或作为内置函数。这些是 Python 生态系统中最基本的,并且是专注于返回布尔状态和具有易于阅读的函数名称的使用风格的好例子。

海峡。方法

isalnum()
isalpha()
isdecimal()
isdigit()
isidentifier()
islower()
isnumeric()
isprintable()
isspace()
istitle()
isupper()

内置函数:

isinstance()
issubclass()