0

I have to written a python script with class and its function. The function parameters shall be varied based on configuration. In 'C' language it can achieved using #ifdef or #if Macro

#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. Because i don't find anything that replace #ifdef in python. i want something like

#ifdef MULTIPLE_APPLICATION
def check_actions(self, machine, application_instance, error_limit)
#else
def check_actions(self, machine, error_limit)
    .......
    .......
Cool_Binami
  • 93
  • 4
  • 12

1 Answers1

2

python is an pure OOP language! There is no #if macros. 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:

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
Adam
  • 2,820
  • 1
  • 13
  • 33
  • @Cool_Binami, this is the closest solution to c like code. why? because with the two inner functions (check_multi_app_action and check_single_app_action) the interpreter when comes to run this code, does not construct them at all, only when the call in the if else is happen the run time constructor build them and bind the functions to their names. so OOP speaking , we don't build objects tell we need them (and functions are objects) good luck – Adam May 02 '20 at 09:34