2014-11-21 41 views
0

我在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有沒有辦法做到這一點。

+0

爲什麼這是一個班呢? – IanAuld 2014-11-21 05:41:38

回答

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) 
+0

內部__dict__屬性是僅獲取屬性的正確方法,因爲dir將返回屬性和方法。 – mdadm 2014-11-21 06:15:19

+0

@mdadm - 不確定我在追隨。 'dir'只是簡單地對'__dict__'屬性的鍵進行過濾和排序(只要不存在'__dir__'方法)。所以,如果'dir'給你方法,那麼'__dict__'也必然會給你。 (請參閱dir上文檔的鏈接)。 – mgilson 2014-11-21 06:48:06

0

你可能想看看您的CLASSE的__dir__屬性,它返回類本身作爲字典

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

試試這個:

print dir(RULES) 

print RULES.__dict__ 

將首先給類,包括它inherites類的屬性的列表。 第二會給你一個字典表示相同

0

你可以使用inspect獲取屬性名稱和值。

import inspect 

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