python rtype docstring/restructured text for class factories/selectors

爷,独闯天下 提交于 2019-12-05 02:35:31

if the data is an instance of SomeAbstractClass, you can use the following snippets:

example 1:

def class_selector(data):
    """
    return: Return the class of data
    """

    return type(data)

example 2:

def class_selector(data):
    """
    return: Return the class of data.
    """

    return data.__class__

And if you want use data.name to specify a class, you can use this:

def class_selector(data):
   """
   return: Return a class specified by data.name
   """
   return type(globals()[data.name])  #or use globals()[data.name].__class__

and you should catch the KeyError exception when you use this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!