2017-04-21 33 views
0

我的需求是基于特定的字符串动态实例化一个类。在这里捕捉到的是新类在其他类上有继承。问题是我无法看到代码从Inherited类中执行。带继承的动态类和方法实例化

我试图通过有一个类SystemConfigure这样做,它将根据字典中给出的参数调用特定的类。在我的代码中,我动态地调用了继承Base类的函数的超类。我没有看到Base类中的代码正在执行。

请让我知道如何做到这一点。

代码
class SystemConfigure(): 

    def __init__(self,snp_dict): 
     dict = snp_dict 
     osname = dict['osname'] 
     protocol = dict['protocol'] 
     module = protocol 
     func_string = osname + "_" + protocol + "_" + "Configure" 
     print ("You have called the Class:", module, "and the function:", func_string) 
     m = globals()[module] 
     func = getattr(m, func_string) 
     func(dict) 

class Base(): 

    def __init__(self): 
     pass 
     print("BASE INIT") 

    def Unix_Base_Configure(dict): 
     print ("GOT IN THE UNIX BASE CLASS FUNCTION") 

    def Linux_Base_Configure(dict): 
     print("GOT IN THE LINUX BASE CLASS FUNCTION") 

class Super(Base): 

    def __init__(self): 
     dict = dict 
     Base.__init__(self) 
     Base.Unix_Base_Configure(dict) 

    def Unix_Super_Configure(dict): 
     print ("GOT IN THE UNIX SUPER CLASS FUNCTION", dict) 

n = SystemConfigure({'protocol':'Super','osname':'Unix','device':'dut'}) 

输出
You have called the Class: Super and the function: Unix_Super_Configure 
GOT IN THE UNIX SUPER CLASS FUNCTION {'protocol': 'Super', 'osname': 'Unix', 'device': 'dut'} 

期望

我期待 “得到了在UNIX基类FUNCTION” 要打印错误。输出需要在“GOT IN THE SUPER CLASS FUNCTION”消息之前打印。

+1

你的代码中有多处错误。你永远不会实例化你的类,所以'__init__'都不会被调用。你的“配置”方法也不接受“自我”论证。 – BrenBarn

回答

0

这通常是Python中的元类的工作。
快速解释,元类可以用来定义'如何'创建一个类。
审查文档或找“蟒蛇元编​​程教程”为主题的更多信息(So: What are Python metaclasses useful for?

class BaseMetaClass(type): 
    def __new__(meta, name, bases, dct): 
     return super(BaseMetaClass, meta).__new__(meta, name, bases, dct) 

    def __init__(cls, name, bases, dct): 
     super(BaseMetaClass, cls).__init__(name, bases, dct) 

    def __call__(cls, *args, **kwds): 
     if args and isinstance(args[0], dict): 
      if 'osname' in args[0]: 
       cls.osname = args[0]['osname'] 
      else: 
       cls.osname = "undefined os" 

      cls.base_configure = "GOT IN THE %s BASE CLASS FUNCTION" % cls.osname.upper() 
     return type.__call__(cls, *args, **kwds) 


class SystemConfigure(metaclass=BaseMetaClass): 
    def __init__(self, snp_dict): 
     print (self.base_configure) 

n = SystemConfigure({'protocol':'Super','osname':'Unix','device':'dut'}) 
n = SystemConfigure({'protocol':'Super','osname':'Linux','device':'dut'}) 
n = SystemConfigure({'protocol':'Super','device':'dut'}) 

回报:

GOT IN THE UNIX BASE CLASS FUNCTION 
GOT IN THE LINUX BASE CLASS FUNCTION 
GOT IN THE WINDOWS BASE CLASS FUNCTION 
0

您需要定义的一些方法为@staticmethods,因为他们不没有self的参数(或需要一个)。下面是你的代码与他们# ADDED

我也改变了如何将字典参数传递给SystemConfigure()被处理,所以它现在利用Python关键字参数传递来创建一个字典来传递它,但这个改变并不是必须的。

class SystemConfigure(): 
    def __init__(self, **kwargs): # CHANGED - argument snp_dict into **kwargs 
     # dict = snp_dict # REMOVED - no longer needed 
     osname = kwargs['osname'] 
     protocol = kwargs['protocol'] 
     module = protocol 
     func_string = osname + "_" + protocol + "_" + "Configure" 
     print ("You have called the Class:", module, "and the function:", func_string) 
     m = globals()[module] 
     func = getattr(m, func_string) 
     func(kwargs) 

class Base(): 
    def __init__(self): 
     pass 
     print("BASE INIT") 

    @staticmethod # ADDED 
    def Unix_Base_Configure(dict): 
     print ("GOT IN THE UNIX BASE CLASS FUNCTION") 

    @staticmethod # ADDED 
    def Linux_Base_Configure(dict): 
     print("GOT IN THE LINUX BASE CLASS FUNCTION") 

class Super(Base): 
    def __init__(self): # THIS IS NEVER CALLED 
#  dict = dict # REMOVED - don't know what this is all about... 
     Base.__init__(self) 
     Base.Unix_Base_Configure(dict) # why is a type being passed? 

    @staticmethod # ADDED 
    def Unix_Super_Configure(dict_): 
     print ("GOT IN THE UNIX SUPER CLASS FUNCTION", dict_) 

# Changed how dictionary argument is created into a more (IMO) readable form (optional) 
n = SystemConfigure(protocol='Super', osname='Unix', device='dut') 

输出:

You have called the Class: Super and the function: Unix_Super_Configure 
GOT IN THE UNIX SUPER CLASS FUNCTION {'protocol': 'Super', 'osname': 'Unix', 'device': 'dut'}