所以我能理解的是你有一个很长的函数,比如:
def long_func(blah, foo, *args):
...
...
my_val = long_func(foo, blah, a, b, c)
你所做的是:
def long_func(blah, foo, *args):
def short_func1():
...
def short_func2():
...
...
short_func1()
short_func2()
...
...
my_val = long_func(foo, blah, a, b, c)
您有更多选择,我将列出两个:
把它变成一个类
class SomeName(object):
def __init__(self, blah, foo, *args):
self.blah = blah
self.foo = foo
self.args = args
self.result = None # Might keep this for returning values or see (2)
def short_func1(self):
...
def short_func2(self):
...
def run(self): # name it as you like!
self.short_func1()
self.short_func2()
return self.result # (2) or return the last call, on you
...
my_val = SomeName(foo, blah, a, b, c).run()
制作另一个模块并将其short_funcs
放入其中。就像 flyx 建议的那样。
def long_func(foo, blah, *args):
from my_module import short_func1, short_func2
short_func1(foo)
short_func2(blah)