简体   繁体   English

定义 function 及其参数基于 python 中的配置

[英]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.

相关问题 Python:如何根据函数的参数创建 csv 列 - Python: how to create a csv column based on a function's parameter 有没有办法通过字符串定义PYthon函数参数列表? - Is there a way to define a PYthon function parameter list by a string? 有没有一种方法可以定义一个变量,该变量是Python函数内部的一个参数? - Is there a way to define a variable that is a parameter inside a function in Python? Python:定义给定函数参数的变量类型 - Python: define type of variable given into a function parameter 是否可以根据Theano中参数的符号定义采用其他形式的函数 - Is it possible to define a function that takes a different form based on the sign of the parameter in Theano 用字符串作为参数定义函数 - define function with strings as parameter 根据先前定义的指定参数的函数定义python函数 - Define a python function based on previously defined function specifying argument 如何使用python在带字符串参数的for循环中定义函数 - How to define a function in a for loop with the string parameter using python 定义draw_histogram()函数,该函数通过Python字典作为参数传递 - Define the draw_histogram() function which is passed a Python dictionary as a parameter 根据python中的参数类型选择函数 - Choosing a function based on parameter types in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM