Python docstring type annotation — a class, not an instance?

前端 未结 5 1115
闹比i
闹比i 2021-01-18 01:23

Let\'s say I have:

class A(object):
   pass

class B(A):
   pass

I want to declare a function that takes a subclass of A as an argument:

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 01:32

    All classes are an instance of the type class:

    >>> class Foo:
    ...     pass
    ... 
    >>> type(Foo())
    
    >>> type(Foo)
    
    >>> type(Foo) is type
    True
    

    So, a valid answer would be:

    :type klass: type
    

提交回复
热议问题