你可以使用dir()函数来获取对象的所有属性和方法,然后通过遍历属性列表来获取每个属性的值。下面是一个示例函数:

def print_attributes(obj):
    attributes = dir(obj)
    for attr in attributes:
        value = getattr(obj, attr)
        if not callable(value):  # 排除方法
            print(f"{attr}: {value}")

这个函数接受一个对象作为参数,并打印出对象的所有非方法属性及其对应的值。你可以根据需要对输出进行格式化或调整。请注意,这个函数会打印出对象的所有属性,包括内置属性和方法。

python 編寫一個函數輸出對象的所有屬性和值

原文地址: https://www.cveoy.top/t/topic/iNVI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录