如何识别变量是类还是对象

时间:2020-12-14 02:38:48

I am working at a bit lower level writing a small framework for creating test fixtures for my project in Python. In this I want to find out whether a particular variable is an instance of a certain class or a class itself and if it is a class, I want to know if it is a subclass of a certain class defined by my framework. How do I do it?

我正在更低层次上编写一个小框架,用于在Python中为我的项目创建测试夹具。在这里我想知道一个特定的变量是某个类的实例还是一个类本身,如果它是一个类,我想知道它是否是我的框架定义的某个类的子类。我该怎么做?

class MyBase(object):
    pass

class A(MyBase):
    a1 = 'Val1'
    a2 = 'Val2'

class B(MyBase):
    a1 = 'Val3'
    a2 = A

I want to find out if the properties a1 and a2 are instances of a class/type (a1 is a string type in B) or a class object itself (i.e a2 is A in B). Can you please help me how do I find this out?

我想知道属性a1和a2是类/类型的实例(a1是B中的字符串类型)还是类对象本身(即a2是B中的A)。你能帮帮我怎样才能找到它?

4 个解决方案

#1


11  

Use the inspect module.

使用检查模块。

The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. For example, it can help you examine the contents of a class, retrieve the source code of a method, extract and format the argument list for a function, or get all the information you need to display a detailed traceback.

inspect模块提供了几个有用的函数来帮助获取有关活动对象的信息,例如模块,类,方法,函数,回溯,框架对象和代码对象。例如,它可以帮助您检查类的内容,检索方法的源代码,提取和格式化函数的参数列表,或获取显示详细回溯所需的所有信息。

For example, the inspect.isclass() function returns true if the object is a class:

例如,如果对象是类,则inspect.isclass()函数返回true:

>>> import inspect
>>> inspect.isclass(inspect)
False
>>> inspect.isclass(inspect.ArgInfo)
True
>>> 

#2


4  

Use isinstance and type, types.ClassType (that latter is for old-style classes):

使用isinstance和type,types.ClassType(后者用于旧式类):

>>> isinstance(int, type)
True

>>> isinstance(1, type)
False

#3


3  

Use the type() function. It will return the type of the object. You can get 'stock' types to match with from the types library. Old-style classes (that don't inherit from anything) have the type types.ClassType. New-style classes like in your example have the type types.TypeType. There are plenty of other useful types in this module for strings and such.

使用type()函数。它将返回对象的类型。您可以从类型库中获取“库存”类型以匹配。旧式类(不从任何东西继承)具有类型types.ClassType。像您的示例中的新式类具有类型types.TypeType。此模块中还有许多其他有用的类型用于字符串等。

Calling type() on an instance of an old-style class returns types.InstanceType. Calling it on an instance of a new-style class returns the class itself.

在旧式类的实例上调用type()会返回types.InstanceType。在新样式类的实例上调用它将返回类本身。

#4


1  

Use type()

使用type()

>>> class A: pass
>>> print type(A)
<type 'classobj'>
>>> A = "abc"
>>> print type(A)
<type 'str'>

#1


11  

Use the inspect module.

使用检查模块。

The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. For example, it can help you examine the contents of a class, retrieve the source code of a method, extract and format the argument list for a function, or get all the information you need to display a detailed traceback.

inspect模块提供了几个有用的函数来帮助获取有关活动对象的信息,例如模块,类,方法,函数,回溯,框架对象和代码对象。例如,它可以帮助您检查类的内容,检索方法的源代码,提取和格式化函数的参数列表,或获取显示详细回溯所需的所有信息。

For example, the inspect.isclass() function returns true if the object is a class:

例如,如果对象是类,则inspect.isclass()函数返回true:

>>> import inspect
>>> inspect.isclass(inspect)
False
>>> inspect.isclass(inspect.ArgInfo)
True
>>> 

#2


4  

Use isinstance and type, types.ClassType (that latter is for old-style classes):

使用isinstance和type,types.ClassType(后者用于旧式类):

>>> isinstance(int, type)
True

>>> isinstance(1, type)
False

#3


3  

Use the type() function. It will return the type of the object. You can get 'stock' types to match with from the types library. Old-style classes (that don't inherit from anything) have the type types.ClassType. New-style classes like in your example have the type types.TypeType. There are plenty of other useful types in this module for strings and such.

使用type()函数。它将返回对象的类型。您可以从类型库中获取“库存”类型以匹配。旧式类(不从任何东西继承)具有类型types.ClassType。像您的示例中的新式类具有类型types.TypeType。此模块中还有许多其他有用的类型用于字符串等。

Calling type() on an instance of an old-style class returns types.InstanceType. Calling it on an instance of a new-style class returns the class itself.

在旧式类的实例上调用type()会返回types.InstanceType。在新样式类的实例上调用它将返回类本身。

#4


1  

Use type()

使用type()

>>> class A: pass
>>> print type(A)
<type 'classobj'>
>>> A = "abc"
>>> print type(A)
<type 'str'>