我有来自不同函数的输出,需要对其进行细微的分析,但是最好的选择不是为每种类型的输出创建多个函数。
我已经审查了许多有关Python 3条件语句的站点,但是还没有找到一种基于调用它的函数创建T或F条件语句的方法。
def function1():
some code
general_function(resultOfSomeCode)
def function2():
some code
general_function(resultOfSomeCode)
def general_function(resultOfSomeCode):
if general_function was called by function1:
do this
elif general_function was called by function2:
do this
我不确定这是否可行,或者是否有最佳做法。我总是可以根据需要解析输出的方式调用多个函数,但是对编程较新的人希望了解如何处理。建议或指导表示赞赏。
答案 0 :(得分:2)
这是一种方法。
def function1():
some code
general_function(resultOfSomeCode, function1.__name__)
def function2():
some code
general_function(resultOfSomeCode, function2.__name__)
def general_function(resultOfSomeCode, calling_func_name):
if calling_func_name__ == 'function1':
do this
elif calling_func_name__ == 'function2':
do this
尽管在general_func
中传递定义您想要的行为的参数而不是调用函数本身会更清洁。