如何在python中将类的所有属性作为列表

时间:2014-11-21 05:24:38

标签: python

我在Python中有以下类。

class RULES(object):
    TOTAL_REQUESTS = 'totalrequests'
    HTML_SIZE = 'htmlsize'
    JS_SIZE = 'jssize'
    JS_COUNT = 'jscount'
    DOMAIN_COUNT = 'domaincount'
    REQ_PER_DOMAIN = 'reqperdomain'
    DOMAIN_WITH_MAX_REQUESTS = 'domainwithmaxrequests'
    IMAGE_SIZE = 'imagesize'
    IMAGE_COUNT = 'imagecount'
    HIGHEST_LATENCY_OBJECT = 'highestlatencyobject'
    LARGEST_SIZE_OBJECT = 'largestsizeobject'
    VARY_HEADER_OBJECTS = 'varyheaderobjects'
    ERROR_RESPONSES = 'errorresponses'
    COOKIE_REQUESTS = 'cookierequests'
    TTFB_REQUESTS = 'ttfb_requests'
    UNCACHED = 'uncached'
    UNCOMPRESSED = 'uncompressed'
    SPRITABLE = 'spritable'
    MULTIPLE_REQUESTS = 'multiplerequests'
    INLINABLE = 'inlinable'
    NO_SERVER_CACHE = 'noservercache'
    MORE_TTL = 'morettl'
    MEDIAN_LOAD_TIME = 'median_load_time'
    AVG_LOAD_TIME = 'avgloadtime'
    DNS = 'dns'
    HOST_TTL = 'host_ttl'
    ORIGIN_TTL = 'origin_ttl'
    THIRD_PARTY_HTTPS_REQ = 'thirdpartyhttpsreq'

现在我需要编写一个迭代所有属性的方法并将它们作为列表返回。我尝试将个别属性视为

RULES.HOST_TTL,但我不确定如何迭代它们。在python中有没有办法做到这一点。

5 个答案:

答案 0 :(得分:3)

您也可以使用class_name.__dict__

>>> class RULES(object):
...     TOTAL_REQUESTS = 'totalrequests'
...     HTML_SIZE = 'htmlsize'
...     JS_SIZE = 'jssize'
...     JS_COUNT = 'jscount'
...     DOMAIN_COUNT = 'domaincount'
...     REQ_PER_DOMAIN = 'reqperdomain'
...     DOMAIN_WITH_MAX_REQUESTS = 'domainwithmaxrequests'
...     IMAGE_SIZE = 'imagesize'
...     IMAGE_COUNT = 'imagecount'
...     HIGHEST_LATENCY_OBJECT = 'highestlatencyobject'
...     LARGEST_SIZE_OBJECT = 'largestsizeobject'
...     VARY_HEADER_OBJECTS = 'varyheaderobjects'
...     ERROR_RESPONSES = 'errorresponses'
...     COOKIE_REQUESTS = 'cookierequests'
...     TTFB_REQUESTS = 'ttfb_requests'
...     UNCACHED = 'uncached'
...     UNCOMPRESSED = 'uncompressed'
...     SPRITABLE = 'spritable'
...     MULTIPLE_REQUESTS = 'multiplerequests'
...     INLINABLE = 'inlinable'
...     NO_SERVER_CACHE = 'noservercache'
...     MORE_TTL = 'morettl'
...     MEDIAN_LOAD_TIME = 'median_load_time'
...     AVG_LOAD_TIME = 'avgloadtime'
...     DNS = 'dns'
...     HOST_TTL = 'host_ttl'
...     ORIGIN_TTL = 'origin_ttl'
...     THIRD_PARTY_HTTPS_REQ = 'thirdpartyhttpsreq'
... 
>>> RULES.__dict__
dict_proxy({'REQ_PER_DOMAIN': 'reqperdomain', '__module__': '__main__', 'JS_COUNT': 'jscount', 'AVG_LOAD_TIME': 'avgloadtime', 'DOMAIN_WITH_MAX_REQUESTS': 'domainwithmaxrequests', 'UNCACHED': 'uncached', 'NO_SERVER_CACHE': 'noservercache', 'HOST_TTL': 'host_ttl', 'MULTIPLE_REQUESTS': 'multiplerequests', 'MORE_TTL': 'morettl', 'DNS': 'dns', 'SPRITABLE': 'spritable', '__dict__': <attribute '__dict__' of 'RULES' objects>, 'LARGEST_SIZE_OBJECT': 'largestsizeobject', 'TTFB_REQUESTS': 'ttfb_requests', 'TOTAL_REQUESTS': 'totalrequests', '__weakref__': <attribute '__weakref__' of 'RULES' objects>, 'ORIGIN_TTL': 'origin_ttl', 'VARY_HEADER_OBJECTS': 'varyheaderobjects', 'MEDIAN_LOAD_TIME': 'median_load_time', 'COOKIE_REQUESTS': 'cookierequests', 'DOMAIN_COUNT': 'domaincount', 'THIRD_PARTY_HTTPS_REQ': 'thirdpartyhttpsreq', 'HTML_SIZE': 'htmlsize', 'UNCOMPRESSED': 'uncompressed', 'HIGHEST_LATENCY_OBJECT': 'highestlatencyobject', 'INLINABLE': 'inlinable', 'ERROR_RESPONSES': 'errorresponses', 'IMAGE_SIZE': 'imagesize', 'JS_SIZE': 'jssize', 'IMAGE_COUNT': 'imagecount', '__doc__': None})

过滤

变量名称和值:

>>> { k:v for k,v in RULES.__dict__.iteritems() if not k.startswith('__')  }
{'JS_COUNT': 'jscount', 'DOMAIN_WITH_MAX_REQUESTS': 'domainwithmaxrequests', 'UNCACHED': 'uncached', 'MULTIPLE_REQUESTS': 'multiplerequests', 'SPRITABLE': 'spritable', 'TTFB_REQUESTS': 'ttfb_requests', 'ORIGIN_TTL': 'origin_ttl', 'VARY_HEADER_OBJECTS': 'varyheaderobjects', 'MEDIAN_LOAD_TIME': 'median_load_time', 'HTML_SIZE': 'htmlsize', 'UNCOMPRESSED': 'uncompressed', 'ERROR_RESPONSES': 'errorresponses', 'IMAGE_SIZE': 'imagesize', 'AVG_LOAD_TIME': 'avgloadtime', 'REQ_PER_DOMAIN': 'reqperdomain', 'NO_SERVER_CACHE': 'noservercache', 'HOST_TTL': 'host_ttl', 'JS_SIZE': 'jssize', 'DNS': 'dns', 'LARGEST_SIZE_OBJECT': 'largestsizeobject', 'DOMAIN_COUNT': 'domaincount', 'TOTAL_REQUESTS': 'totalrequests', 'COOKIE_REQUESTS': 'cookierequests', 'IMAGE_COUNT': 'imagecount', 'MORE_TTL': 'morettl', 'HIGHEST_LATENCY_OBJECT': 'highestlatencyobject', 'INLINABLE': 'inlinable', 'THIRD_PARTY_HTTPS_REQ': 'thirdpartyhttpsreq'}

变量名称:

>>> [ k for k,v in RULES.__dict__.iteritems() if not k.startswith('__')  ]
['REQ_PER_DOMAIN', 'JS_COUNT', 'AVG_LOAD_TIME', 'DOMAIN_WITH_MAX_REQUESTS', 'UNCACHED', 'NO_SERVER_CACHE', 'HOST_TTL', 'MULTIPLE_REQUESTS', 'MORE_TTL', 'DNS', 'SPRITABLE', 'LARGEST_SIZE_OBJECT', 'TTFB_REQUESTS', 'TOTAL_REQUESTS', 'ORIGIN_TTL', 'VARY_HEADER_OBJECTS', 'MEDIAN_LOAD_TIME', 'COOKIE_REQUESTS', 'DOMAIN_COUNT', 'THIRD_PARTY_HTTPS_REQ', 'HTML_SIZE', 'UNCOMPRESSED', 'HIGHEST_LATENCY_OBJECT', 'INLINABLE', 'ERROR_RESPONSES', 'IMAGE_SIZE', 'JS_SIZE', 'IMAGE_COUNT']

答案 1 :(得分:1)

使用dir功能。它可能会比你想要的更多,但你可以过滤它(例如只需要ALL_CAPS单词)。

样品:

>>> class RULES(object):
...     TOTAL_REQUESTS = 'totalrequests'
...     HTML_SIZE = 'htmlsize'
...     JS_SIZE = 'jssize'
...     JS_COUNT = 'jscount'
...     DOMAIN_COUNT = 'domaincount'
...     REQ_PER_DOMAIN = 'reqperdomain'
...     DOMAIN_WITH_MAX_REQUESTS = 'domainwithmaxrequests'
...     IMAGE_SIZE = 'imagesize'
...
>>> import re
>>> all_caps = re.compile('^[A-Z_]+$')
>>> [attr for attr in dir(RULES) if all_caps.match(attr)]
['DOMAIN_COUNT', 'DOMAIN_WITH_MAX_REQUESTS', 'HTML_SIZE', 'IMAGE_SIZE', 'JS_COUNT', 'JS_SIZE', 'REQ_PER_DOMAIN', 'TOTAL_REQUESTS']

一旦你拥有名称,就很容易获得价值:

[getattr(RULES, attr) for attr in dir(fules) if all_caps.match(attr)]

最后,如果您可以使用其他类型,则可以在此处使用Enum。在python3.4之前,enum没有添加到标准库中,但python2.x有backports。我相信您的请求会如下:

class RULES(enum.Enum):
    X = 'foo'
    Y = 'bar'
    # ...

 for name, attr in RULES.__members__.items():
     print(name, attr.value)

答案 2 :(得分:1)

试试这个:

print dir(RULES)

print RULES.__dict__

首先给出一个类的属性列表,包括它继承的类。 第二个将为您提供相同的字典表示

答案 3 :(得分:0)

你可能想查看你的classe的__dir__属性,它将类本身作为字典返回

答案 4 :(得分:0)

您可以使用inspect获取属性名称和值。

import inspect

all_attr = inspect.getmembers(RULES)
[attr for attr in all_attr if not(attr[0].startswith('__') and attr[0].endswith('__'))]