代码之家  ›  专栏  ›  技术社区  ›  Christian Oudard

python 3中的types.classtype发生了什么?

  •  14
  • Christian Oudard  · 技术社区  · 16 年前

    我有一个脚本,在其中我做一些神奇的事情来动态加载模块,并实例化模块中找到的第一个类。但我不能用 types.ClassType 在python 3中。现在正确的方法是什么?

    2 回复  |  直到 16 年前
        1
  •  16
  •   Christian Oudard    16 年前

    我知道了。类的类型似乎是“类型”。下面是一个如何在运行时区分类和其他对象的示例。

    >>> class C: pass
    ... 
    >>> type(C)
    <class 'type'>
    >>> isinstance(C, type)
    True
    >>> isinstance('string', type)
    False
    
        2
  •  5
  •   Keith Pinson sumit vedi    13 年前

    它被用来 classic classes . 在python 3中,它们不见了。 我想你可以用如下的方法:

    issubclass(ClassName, object)