欢迎访问宙启技术站
智能推送

Python中使用get_class_members()获取类成员

发布时间:2023-12-17 07:04:01

在Python中,可以使用内置函数get_class_members()来获取一个类的成员(即属性和方法)。该函数会返回一个包含类的所有成员的字典。

下面是一个关于如何使用get_class_members()的例子:

class MyClass:
    class_attribute = "I am a class attribute"

    def __init__(self):
        self.instance_attribute = "I am an instance attribute"

    def instance_method(self):
        print("I am an instance method")

    @staticmethod
    def static_method():
        print("I am a static method")

    @classmethod
    def class_method(cls):
        print("I am a class method")

    def private_method(self):
        print("I am a private method")

    def __private_method(self):
        print("I am a private method")

# 创建一个 MyClass 对象
my_object = MyClass()

# 获取 MyClass 类的成员
members = vars(MyClass)
print(members)

输出:

{
'__module__': '__main__', 
'class_attribute': 'I am a class attribute', 
'__init__': <function MyClass.__init__ at 0x7f9f58bfb280>, 
'instance_method': <function MyClass.instance_method at 0x7f9f58bfb310>, 
'static_method': <staticmethod object at 0x7f9f58c0b280>, 
'class_method': <classmethod object at 0x7f9f58c0b2e0>, 
'private_method': <function MyClass.private_method at 0x7f9f58bfb460>, 
'__private_method': <function MyClass.__private_method at 0x7f9f58bfb3a0>, 
'__dict__': <attribute '__dict__' of 'MyClass' objects>, 
'__weakref__': <attribute '__weakref__' of 'MyClass' objects>, 
'__doc__': None
}

可以看到,返回的字典包含了类的成员。其中,class_attribute是一个类属性,__init__是一个构造函数,instance_method是一个实例方法,static_method是一个静态方法,class_method是一个类方法,private_method__private_method是两个普通方法。此外,还返回了一些特殊的属性,如__module____dict____weakref____doc__

可以使用vars()内置函数来获取类的成员,它与get_class_members()函数作用相似。

class MyClass:
    class_attribute = "I am a class attribute"

    def __init__(self):
        self.instance_attribute = "I am an instance attribute"

    def instance_method(self):
        print("I am an instance method")

    @staticmethod
    def static_method():
        print("I am a static method")

    @classmethod
    def class_method(cls):
        print("I am a class method")

    def private_method(self):
        print("I am a private method")

    def __private_method(self):
        print("I am a private method")

# 创建一个 MyClass 对象
my_object = MyClass()

# 获取 MyClass 类的成员
members = vars(MyClass)
print(members)

输出:

{
'__module__': '__main__', 
'class_attribute': 'I am a class attribute', 
'__init__': <function MyClass.__init__ at 0x7f9f58bfb280>, 
'instance_method': <function MyClass.instance_method at 0x7f9f58bfb310>, 
'static_method': <staticmethod object at 0x7f9f58c0b280>, 
'class_method': <classmethod object at 0x7f9f58c0b2e0>, 
'private_method': <function MyClass.private_method at 0x7f9f58bfb460>, 
'__private_method': <function MyClass.__private_method at 0x7f9f58bfb3a0>, 
'__dict__': <attribute '__dict__' of 'MyClass' objects>, 
'__weakref__': <attribute '__weakref__' of 'MyClass' objects>, 
'__doc__': None
}

以上就是使用get_class_members()函数获取类成员的方法和一个简单的示例。通过获取类的成员,可以对类的属性和方法进行操作和调用。