首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python -遍历属性列表

Python中,遍历属性列表可以通过使用内置函数dir()getattr()来实现。

dir()函数返回一个包含对象所有属性和方法名称的列表。可以将这个列表作为遍历的目标,然后使用getattr()函数获取每个属性的值。

以下是一个示例代码:

代码语言:txt
复制
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

person = Person("Alice", 25)

# 使用dir()函数获取属性列表
attributes = dir(person)

# 遍历属性列表并获取属性值
for attribute in attributes:
    value = getattr(person, attribute)
    print(f"{attribute}: {value}")

输出结果将会是:

代码语言:txt
复制
__class__: <class '__main__.Person'>
__delattr__: <method-wrapper '__delattr__' of Person object at 0x7f8a2c3b7a90>
__dict__: {'name': 'Alice', 'age': 25}
__dir__: <built-in method __dir__ of Person object at 0x7f8a2c3b7a90>
__doc__: None
__eq__: <method-wrapper '__eq__' of Person object at 0x7f8a2c3b7a90>
__format__: <built-in method __format__ of Person object at 0x7f8a2c3b7a90>
__ge__: <method-wrapper '__ge__' of Person object at 0x7f8a2c3b7a90>
__getattribute__: <method-wrapper '__getattribute__' of Person object at 0x7f8a2c3b7a90>
__gt__: <method-wrapper '__gt__' of Person object at 0x7f8a2c3b7a90>
__hash__: <method-wrapper '__hash__' of Person object at 0x7f8a2c3b7a90>
__init__: <bound method Person.__init__ of <__main__.Person object at 0x7f8a2c3b7a90>>
__init_subclass__: <built-in method __init_subclass__ of type object at 0x557e8e4e4f50>
__le__: <method-wrapper '__le__' of Person object at 0x7f8a2c3b7a90>
__lt__: <method-wrapper '__lt__' of Person object at 0x7f8a2c3b7a90>
__module__: __main__
__ne__: <method-wrapper '__ne__' of Person object at 0x7f8a2c3b7a90>
__new__: <built-in method __new__ of type object at 0x557e8e4e4f50>
__reduce__: <built-in method __reduce__ of Person object at 0x7f8a2c3b7a90>
__reduce_ex__: <built-in method __reduce_ex__ of Person object at 0x7f8a2c3b7a90>
__repr__: <method-wrapper '__repr__' of Person object at 0x7f8a2c3b7a90>
__setattr__: <method-wrapper '__setattr__' of Person object at 0x7f8a2c3b7a90>
__sizeof__: <built-in method __sizeof__ of Person object at 0x7f8a2c3b7a90>
__str__: <method-wrapper '__str__' of Person object at 0x7f8a2c3b7a90>
__subclasshook__: <built-in method __subclasshook__ of type object at 0x557e8e4e4f50>
__weakref__: None
age: 25
name: Alice

在这个例子中,我们定义了一个Person类,它有nameage两个属性。我们创建了一个Person对象,并使用dir()函数获取属性列表。然后,我们使用getattr()函数遍历属性列表,并获取每个属性的值进行打印。

需要注意的是,dir()函数返回的列表中包含了一些特殊属性和方法,如__class____dict____doc__等。这些属性和方法是Python对象的内置属性和方法,不是我们自定义的属性。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • iOS Category实现原理

    // Attach method lists and properties and protocols from categories to a class. // Assumes the categories in cats are all loaded and sorted by load order, // oldest categories first. static void attachCategories(Class cls, category_list *cats, bool flush_caches) { if (!cats) return; if (PrintReplacedMethods) printReplacements(cls, cats); bool isMeta = cls->isMetaClass(); // fixme rearrange to remove these intermediate allocations method_list_t **mlists = (method_list_t **) malloc(cats->count * sizeof(*mlists)); property_list_t **proplists = (property_list_t **) malloc(cats->count * sizeof(*proplists)); protocol_list_t **protolists = (protocol_list_t **) malloc(cats->count * sizeof(*protolists)); // Count backwards through cats to get newest categories first int mcount = 0; int propcount = 0; int protocount = 0; int i = cats->count; bool fromBundle = NO; while (i--) { auto& entry = cats->list[i]; method_list_t *mlist = entry.cat->methodsForMeta(isMeta); if (mlist) { mlists[mcount++] = mlist; fromBundle |= entry.hi->isBundle(); } property_list_t *proplist = entry.cat->propertiesForMeta(isMeta, entry.hi); if (proplist) { proplists[propcount++] = proplist; } protocol_list_t *protolist = entry.cat->protocols; if (protolist) { protolists[protocount++] = protolist; } } auto rw = cls->data(); prepareMethodLists(cls, mlists, mcount, NO, fromBundle); rw->methods.attachLists(mlists, mcount); free(mlists); if (flush_caches && mcount > 0) flushCaches(cls); rw->properties.attachLists(proplists, propcount); free(proplists); rw->protocols.attachLists(protolists, protocount); free(protolists); }

    02

    Spring IOC 容器源码分析 - 填充属性到 bean 原始对象

    本篇文章,我们来一起了解一下 Spring 是如何将配置文件中的属性值填充到 bean 对象中的。我在前面几篇文章中介绍过 Spring 创建 bean 的流程,即 Spring 先通过反射创建一个原始的 bean 对象,然后再向这个原始的 bean 对象中填充属性。对于填充属性这个过程,简单点来说,JavaBean 的每个属性通常都有 getter/setter 方法,我们可以直接调用 setter 方法将属性值设置进去。当然,这样做还是太简单了,填充属性的过程中还有许多事情要做。比如在 Spring 配置中,所有属性值都是以字符串的形式进行配置的,我们在将这些属性值赋值给对象的成员变量时,要根据变量类型进行相应的类型转换。对于一些集合类的配置,比如

    02
    领券