日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python调用函数示例_python 动态调用函数实例解析

發布時間:2024/1/23 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python调用函数示例_python 动态调用函数实例解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 根據字符串名稱 動態調用 python文件內的方法eval("function_name")(參數)

2. 根據字符串 動態調用類中的靜態方法,getattr(ClassName,"function_name")(參數)

3. apply(functoin_name,parameters) 這個function_name不是字符串,而是函數對象本身;parameters是參數,類似(a,b,...)這樣的格式

4. 當函數不確定參數的數目時候,采用 一個 * 或兩個** 他們的用法是有講究的。

下面的例子是,定義了一個函數列表字典,字典中保存有函數對象和函數的參數,可以實現動態為字典添加執行的函數,最后一起執行

from collections import OrderedDict

class ComponentCheck:

def __init__(self, data_dir):

self.data_dir = data_dir

self._extend_function_dic = OrderedDict({})

def add_extend_function(self, function_name, *parameters):

self._extend_function_dic[function_name] = parameters

def _check_extend_function(self):

for function_name, parameters in self._extend_function_dic.iteritems():

if not apply(function_name, parameters):

return False

return True

class CheckFunctions:

def __init__(self):

pass

def tollcost_check(data_path):

toll_cost_path = os.path.join(data_path, Importer.DT_KOR_TOLL_COST)

tollcost_component = ComponentCheck(toll_cost_path)

tollcost_component.add_extend_function(tollcost_component.check_file_pattern_list_match, CheckFunctions.TOLL_COST_FILENAME_PATTERN)

return tollcost_component

@staticmethod

def speed_camera_check(data_path):

speed_camera_path = os.path.join(data_path, Importer.DT_SAFETY_CAMERA)

speed_camera_component = ComponentCheck(speed_camera_path)

speed_camera_component.add_extend_function(speed_camera_component.check_not_exist_empty_directory)

return speed_camera_component

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的python调用函数示例_python 动态调用函数实例解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。