如何列出所有类属性 [英] How to list all class properties

查看:56
本文介绍了如何列出所有类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些带属性的类 SomeClass.例如 idname:

I have class SomeClass with properties. For example id and name:

class SomeClass(object):
    def __init__(self):
        self.__id = None
        self.__name = None

    def get_id(self):
        return self.__id

    def set_id(self, value):
        self.__id = value

    def get_name(self):
        return self.__name

    def set_name(self, value):
        self.__name = value

    id = property(get_id, set_id)
    name = property(get_name, set_name)

列出属性的最简单方法是什么?我需要这个进行序列化.

What is the easiest way to list properties? I need this for serialization.

推荐答案

property_names=[p for p in dir(SomeClass) if isinstance(getattr(SomeClass,p),property)]

这篇关于如何列出所有类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆