只是一个简单的贡献。如果我们需要实例的类在同一个文件中,我们可以使用这样的东西:# Get class from globals and create an instancem = globals()['our_class']()# Get the function (from the instance) that we need to callfunc = getattr(m, 'function_name')# Call itfunc()例如:class A: def __init__(self): pass def sampleFunc(self, arg): print('you called sampleFunc({})'.format(arg))m = globals()['A']()func = getattr(m, 'sampleFunc')func('sample arg')# Sample, all on one linegetattr(globals()['A'](), 'sampleFunc')('sample arg')而且,如果不是一个类:def sampleFunc(arg): print('you called sampleFunc({})'.format(arg))globals()['sampleFunc']('sample arg')