[英]Define function and it's parameter based on configuration in python
I have to written a python script with class and its function.我必须用 class 及其 function 编写 python 脚本。 The function parameters shall be varied based on configuration.
function 参数应根据配置而变化。 In 'C' language it can achieved using #ifdef or #if Macro
在“C”语言中,它可以使用#ifdef 或#if 宏来实现
#ifdef MULTIPLE_APPLICATION
uint8 check_actions(int machine, int application_instance, int error_limit)
{
.....
}
#else /*single applciation*/
uint8 check_actions(int machine, int error_limit)
{
.....
}
In same way how can i achieve in python.同样,我如何在 python 中实现。 Because i don't find anything that replace #ifdef in python.
因为我在 python 中找不到任何替换 #ifdef 的东西。 i want something like
我想要类似的东西
#ifdef MULTIPLE_APPLICATION
def check_actions(self, machine, application_instance, error_limit)
#else
def check_actions(self, machine, error_limit)
.......
.......
python is an pure OOP language. python 是纯 OOP 语言。 There is no #if macros.
没有#if 宏。 the consipt is totaly diffrent here.
这里的情况完全不同。 you can solve it in many ways.
您可以通过多种方式解决它。 one of the simple ways is to define outer function and 2 nested inner functions and call one of the inner functions coresponding to var that you pass to the outer functions: see below:
一种简单的方法是定义外部 function 和 2 个嵌套内部函数,并调用与传递给外部函数的 var 对应的内部函数之一:见下文:
def check_action(application_stat, *args, **kwargs):
def check_single_app_action(*args, **kwargs):
#do some thing
return
def check_multi_app_action(*args, **kwargs):
#do some work
return
if application_stat=='single':
check_single_app_action(args, kwargs)
else:
check_multi_app_action(args, kwargs)
return
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.