python 3中的types.ClassType发生了什么? [英] What happened to types.ClassType in python 3?

查看:117
本文介绍了python 3中的types.ClassType发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,我在其中执行一些魔术操作以动态加载模块,并实例化模块中找到的第一个类。但是我无法在Python 3中使用 types.ClassType 了。现在正确的方法是什么?

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?

推荐答案

我知道了。看来类的类型是类型。这是一个如何在运行时区分类和其他对象的示例。

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

这篇关于python 3中的types.ClassType发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆