class Catalog:
def __init__(self, param):
self._static_method_class = {'param_value_1': self._static_method_1, 'param_value_2': self._static_method_2}
if param in self._static_method_class:
self.param = param
else:
raise ValueError('Invalid value for param: {0}'.format(param))
@staticmethod
def _static_method_1(self):
print('excuted method 1')
@staticmethod
def _static_method_2(self):
print('excuted method 2')
def main_method(self):
self._static_method_class[self.param](self)
class CatalogInstance:
def __init__(self, param):
self.x1 = 'x1'
self.x2 = 'x2'
if param in self._instance_method_choices:
self.param = param
else:
raise ValueError('Invalid value for param: {0}'.format(param))
def _instance_method_1(self):
print('Value {}'.format(self.x1))
def _instance_method_2(self):
print('Value {}'.format(self.x2))
_instance_method_choices = {'param_value_1': _instance_method_1, 'param_value_2': _instance_method_2}
def main_method(self):
self._instance_method_choices[self.param](self)
class CatalogClass:
x1 = 'x1'
x2 = 'x2'
def __init__(self, param):
if param in self._static_method_choices:
self.param = param
else:
raise ValueError('Invalid Value for param: {0}'.format(param))
@classmethod
def _static_method_1(cls):
print('executed method 1')
@classmethod
def _static_method_2(cls):
print('executed method 2')
_static_method_choices = {'param_value_1': _static_method_1, 'param_value_2': _static_method_2}
def main_method(self):
self._static_method_choices[self.param].__get__(None, self.__class__)()
class CatalogStatic:
def __init__(self, param):
if param in self._static_method_choices:
self.param = param
else:
raise ValueError('Invalid Value for param: {0}'.format(param))
@staticmethod
def _static_method_1():
print('executed method 1')
@staticmethod
def _static_method_2():
print('executed method 2')
_static_method_choices = {'param_value_1': _static_method_1, 'param_value_2': _static_method_2}
def main_method(self):
self._static_method_choices[self.param].__get__(None, self.__class__)()
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69983372/viewspace-2722214/ ,如需转载,请注明出处,否则将追究法律责任。