python 打印类所有属性和方法

class A():
    def __init__(self):
        self._haha = 1

    def cs(self):
        return 10

    def cs2(self):
        return 20034

def print_object(obj):
    print("打印类所有属性和方法下 " *5)
    # print('\n'.join(['%s:%s' % item for item in obj.__dict__.items()]))
    for item in dir(obj):  # obj是某类的一个实例化对象
        print(item)
    print("打印类所有属性和方法上 " * 5)

a = A()
print_object(a)

输出

D:\Anaconda\envs\yolov5_src\python.exe D:/webots/lib/controller/python37/cs1.py
打印类所有属性和方法下 打印类所有属性和方法下 打印类所有属性和方法下 打印类所有属性和方法下 打印类所有属性和方法下 
__class__
__delattr__
__dict__
__dir__
__doc__
__eq__
__format__
__ge__
__getattribute__
__gt__
__hash__
__init__
__init_subclass__
__le__
__lt__
__module__
__ne__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__sizeof__
__str__
__subclasshook__
__weakref__
_haha
cs
cs2
打印类所有属性和方法上 打印类所有属性和方法上 打印类所有属性和方法上 打印类所有属性和方法上 打印类所有属性和方法上 

进程已结束,退出代码 0

你可能感兴趣的:(python小代码记录,python,开发语言)