-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpdw_009_attributedict.py
More file actions
37 lines (30 loc) · 936 Bytes
/
pdw_009_attributedict.py
File metadata and controls
37 lines (30 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
pdw_id = 9
title = "Attributedict: dictionary whose keys are also attributes"
author = "Kurt"
pub_date = (2010, 12, 7, 16, 29)
tags = ('dict','attributes')
"""
No doubt Javascripters out there will find this construct pretty familiar.
"""
class attributedict(dict):
def __init__(self, *a, **kw):
self.__dict__ = self
dict.__init__(self, *a, **kw)
"""
attributedict is a dictionary whose keys are also accessible as object attributes.
This incredibly simple recipe is probably one of my favorites. For most intents
and purposes, it supplants the whole `bunch Python package <http://pypi.python.org/pypi/bunch>`_
in four lines.
Let's see it in action:
>>> ad = attributedict()
>>> ad["one"] = 1
>>> ad.one
1
>>> ad.two = 2
>>> ad["two"]
2
>>> attributedict(three=3).three
3
*Update*: we found `a similar recipe on ActiveState code`__. From 2005!
__ http://code.activestate.com/recipes/361668/
"""