동적 분석 함수 호출 파이썬 예

  1. 파이썬 파일 ( "FUNCTION_NAME") 내에서 동적 호출 방법 평가 문자열 이름 (매개 변수)

  2. 동적 호출 스트링 클래스 정적 방법 getattr (클래스 명 "FUNCTION_NAME") (파라미터)

  3. (functoin_name 파라미터)이없는 문자열 FUNCTION_NAME하지만 객체 자체의 함수 적용; 매개 변수는 형식 (... 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

우리는 노인이 학습 방법을 보려면 사이트를 학습 파이썬을 추천합니다! 기본 파이썬 스크립트, 파충류,에서 장고, 데이터 마이닝,뿐만 아니라 전투에 프로그래밍 기법을 제로 분류 데이터 항목, 모든 사랑 학습 파이썬 작은 파트너에게 주어진! 파이썬 베테랑 하루 배우고 작은 세부 사항에주의 할 필요가 클릭에서 우리와 함께 할 수있는 몇 가지 방법을 공유하고, 기술의 타이밍을 설명해야 학습자 수집 파이썬

출시 세 원저 · 원의 칭찬 0 · 조회수 856

추천

출처blog.csdn.net/haoxun07/article/details/104450716