What happened to types.ClassType in python 3?

2019-04-04 05:26发布

I have a script where I do some magic stuff to dynamically load a module, and instantiate the first class found in the module. But I can't use types.ClassType anymore in Python 3. What is the correct way to do this now?

2条回答
Melony?
2楼-- · 2019-04-04 05:58

It was used for classic classes. In Python 3 they're gone. I suppose you could use something like:

issubclass(ClassName, object)
查看更多
SAY GOODBYE
3楼-- · 2019-04-04 06:03

I figured it out. It seems that classes are of type "type". Here is an example of how to distinguish between classes and other objects at runtime.

>>> class C: pass
... 
>>> type(C)
<class 'type'>
>>> isinstance(C, type)
True
>>> isinstance('string', type)
False
查看更多
登录 后发表回答