[code lang="python"]
# -*- coding: utf-8 -*-
"""
@Time: 2018/1/8
@Author: songhao
@微信公众号: zeropython
@File: c3.py
"""

def caiji_o():
print("caiji_o")

def caiji_pp():
print("caiji_pp")

def caiji_oo():
print("caiji_oo")

def caiji_ll():
print("caiji_ll")

if __name__ == '__main__':
get_func = [globals()[name] for name in globals() if name.startswith('caiji')]
for x in get_func:
x()
output
#/Users/songhao/.virtualenvs/get/bin/python /Users/songhao/py/get/c3.py
#caiji_o
#caiji_pp
#caiji_oo
#caiji_ll

[/code]

如果从另外一个文件调用呢?
[code lang="python"]
# -*- coding: utf-8 -*-
"""
@Time : 2018/1/8 下午4:50
@Author : Songhao
@微信公众号 : zeropython
@File : c2.py
"""

import types
import c3
# from c3 import *

print(dir(c3))

fl = [x for x in dir(c3) if x.startswith('caiji')]

# print(c3.__dict__)
print([c3.__dict__.get(a) for a in dir(c3) if a.startswith('caiji')])
for x in [c3.__dict__.get(a) for a in dir(c3) if a.startswith('caiji')]:
x()

# 也可以用这种方法
get_func = [globals()[name] for name in globals() if name.startswith('caiji')]
print(get_func)
for x in get_func:
x()
[/code]

python 获取特定名称某些函数-新乡seo|网站优化,网站建设—昊天博客

另外可以通过装饰器,用到什么函数直接用装饰器装饰一下

[code lang="python"]

fs = []
def get_f(func):
fs.append(func)
return func

@get_f
def one():
print("one")

@get_f
def two():
print("two")

for x in fs:
x()

[/code]