[英]Define function and it's parameter based on configuration in python
我必須用 class 及其 function 編寫 python 腳本。 function 參數應根據配置而變化。 在“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)
{
.....
}
同樣,我如何在 python 中實現。 因為我在 python 中找不到任何替換 #ifdef 的東西。 我想要類似的東西
#ifdef MULTIPLE_APPLICATION
def check_actions(self, machine, application_instance, error_limit)
#else
def check_actions(self, machine, error_limit)
.......
.......
python 是純 OOP 語言。 沒有#if 宏。 這里的情況完全不同。 您可以通過多種方式解決它。 一種簡單的方法是定義外部 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.