检查对象的Python属性 [英] inspect attributes of object python

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

问题描述

我在蟒蛇的对象如<亲临/项目/人/ ID> 。现在,我想看到​​的人的所有属性,像我有名字,姓氏和人的称号。我想获得的
{'姓':'安娜','姓氏':'佩里','标题':'小姐'}

I have an object in python like <Person at /project/persons/id>. Now I want to see all the attributes of the person like I have FirstName, LastName and title of the person. What I would like to get is {'FirstName':'Anna', 'LastName': 'Perry', 'Title' : 'Ms.'}.

我试过对象.__字典__ ,但它给了我其他内置属性为好。我只想让用户指定的属性。谁能帮我这个?

I tried object.__dict__ but it gives me other built-in attributes as well. I would only like to get user specified attributes. Can anyone help me with this?

推荐答案

有没有直接的方法只拿到用户定义的属性。通常人们会用dunder名称作为一个信号:

There's no direct way to get only the user-defined attributes. Often people will use dunder names as a signal:

attrs = {}
for k in dir(my_object):
    if k.startswith("__") and k.endswith("__"):
        continue
    attrs[k] = my_object[k]

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

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