Python 作为函数的变量的命名约定

Python naming convention for variables that are functions

Python 函数变量有命名约定吗?我在 PEP-8 中看不到任何具体的内容(除了命名变量)。

由于函数是 Python 中的第一个 class 个对象,是否使用 _fn 后缀或类似的东西,一个公认的约定?

编辑:更新了更真实的例子

示例:

def foo_a():
    print 'a'

def foo_b():
    print 'b'

funcs = {'a': foo_a, 'b': foo_b}

# dynamically select function based on some key
key = 'a'
foo_fn = funcs[key]

Does Python have a naming convention for variables that are functions?

不,不是,函数是 Python 中的第一个 class 个对象。传递函数名称,就像您访问它以进行调用一样。

例如:

def foo():
    pass

foo() # calling

another_function(foo) # passing foo

但是,编程中最困难的事情之一就是正确命名。我当然会使用一个更具描述性的名称,可能是一个动词。例如:

def do_nothing():
    pass

编辑:同理,但没有什么可以阻止您使用 _fn 作为后缀,如果它能让您的代码更清晰的话:

def foo_a():
    print 'a'

def foo_b():
    print 'b'

funcs = {'a': foo_a, 'b': foo_b}

# dynamically select function based on some key
key = 'a'
foo_fn = funcs[key]

foo_fn() # calling
another_function(foo_fn) # passing

函数只是 Python 中 可调用 对象的一个​​子集。 callable 只是一个对象类型,例如 strlistdict。没有命名它们中的任何一个的约定,特别是我看不出为什么这与 callable.

有任何不同的原因