如何在Python中使用get_class_members()函数获取类成员信息
发布时间:2023-12-17 07:09:47
在Python中,可以使用get_class_members()函数来获取一个类的成员信息。get_class_members()函数是inspect模块中的一个函数,它返回一个字典,其中包含了指定类的成员信息。这些成员信息可以是类变量、类方法、实例变量、实例方法等。
下面是一个使用get_class_members()函数获取类成员信息的示例:
import inspect
class MyClass:
class_variable = "Class Variable"
def __init__(self, param1, param2):
self.instance_variable = param1
self.another_instance_variable = param2
def instance_method(self):
print("This is an instance method")
@classmethod
def class_method(cls):
print("This is a class method")
# 使用get_class_members()函数获取MyClass类的成员信息
members = inspect.getmembers(MyClass)
# 遍历成员信息
for member in members:
print(member)
运行以上代码,将会输出以下结果:
('__class__', <class '__main__.MyClass'>)
('__delattr__', <slot wrapper '__delattr__' of 'object' objects>)
('__dict__', <attribute '__dict__' of 'MyClass' objects>)
('__dir__', <method '__dir__' of 'object' objects>)
('__doc__', None)
('__eq__', <slot wrapper '__eq__' of 'object' objects>)
('__format__', <method '__format__' of 'object' objects>)
('__ge__', <slot wrapper '__ge__' of 'object' objects>)
('__getattribute__', <slot wrapper '__getattribute__' of 'object' objects>)
('__gt__', <slot wrapper '__gt__' of 'object' objects>)
('__hash__', <slot wrapper '__hash__' of 'object' objects>)
('__init__', <function MyClass.__init__ at 0x7f980014aae8>)
('__init_subclass__', <built-in method __init_subclass__ of type object at 0x94e4a0>)
('__le__', <slot wrapper '__le__' of 'object' objects>)
('__lt__', <slot wrapper '__lt__' of 'object' objects>)
('__module__', '__main__')
('__ne__', <slot wrapper '__ne__' of 'object' objects>)
('__new__', <built-in method __new__ of type object at 0x55b41f215480>)
('__reduce__', <method '__reduce__' of 'object' objects>)
('__reduce_ex__', <method '__reduce_ex__' of 'object' objects>)
('__repr__', <slot wrapper '__repr__' of 'object' objects>)
('__setattr__', <slot wrapper '__setattr__' of 'object' objects>)
('__sizeof__', <method '__sizeof__' of 'object' objects>)
('__str__', <slot wrapper '__str__' of 'object' objects>)
('__subclasshook__', <built-in method __subclasshook__ of type object at 0x94e4a0>)
('another_instance_variable', 'param2')
('class_method', <classmethod object at 0x7f9800128dd8>)
('class_variable', 'Class Variable')
('instance_method', <bound method MyClass.instance_method of <__main__.MyClass object at 0x7f980013b198>>)
('instance_variable', 'param1')
上述结果中,每个成员信息是一个元组,其中包含成员的名称和对应的值。
需要注意的是,get_class_members()函数会返回所有类型的成员信息,包括内置的成员。如果想要获取自定义类的成员信息,建议使用inspect.getmembers(cls, inspect.isfunction)这个形式的调用,它只返回自定义类的方法成员信息。
接下来让我们通过一个修改后的示例来演示如何只获取自定义类的方法成员信息:
import inspect
class MyClass:
class_variable = "Class Variable"
def __init__(self, param1, param2):
self.instance_variable = param1
self.another_instance_variable = param2
def instance_method(self):
print("This is an instance method")
@classmethod
def class_method(cls):
print("This is a class method")
# 使用get_class_members()函数获取MyClass类的自定义方法成员信息
members = inspect.getmembers(MyClass, inspect.isfunction)
# 遍历成员信息
for member in members:
print(member)
运行以上代码,将会输出以下结果:
('instance_method', <function MyClass.instance_method at 0x7f980014a9d8>)
('class_method', <classmethod object at 0x7f980015fda0>)
以上示例中,我们使用了inspect.isfunction来指定只返回函数类的成员信息。这样就只获取到了MyClass类的自定义方法成员信息,而不包括其他内置的成员信息。
