问题描述
在这种情况下,我有两种方便的方法可以从入口点查找和初始化类,并且想将它们的作用区分开来。
以下是我的问题的简化版本
class ToBeExtended:
""" Abstract base class that is to be extended """
pass
def find(classname: str) -> ToBeExtended:
""" Find the class deFinition of the class named `classname` from the entry points """
... # Get the class
return ClassDeFinition
def connect(classname: str,*args,**kwargs) -> ToBeExtended:
""" Find and initialise the class with the passed arguments and return """
return find(classname)(*args,**kwargs)
在此示例中, find
返回一个类对象,而不是实例。
有没有办法将其包装起来,以便将上下文提供给linter / user? typing
中似乎没有任何内容,我想拥有该类可识别的,没什么像-> type:
解决方法
import typing
class ToBeExtended:
""" Abstract base class that is to be extended """
pass
def find(classname: str) -> typing.Type[ToBeExtended]:
""" Find the class definition of the class named `classname` from the entry points """
... # Get the class
return ClassDefinition
def connect(classname: str,*args,**kwargs) -> ToBeExtended:
""" Find and initialise the class with the passed arguments and return """
return find(classname)(*args,**kwargs)