您還可以使用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']
爲什麼這是一個班呢? – IanAuld 2014-11-21 05:41:38