- List of strings: [name]
- Set of strings: {name}
- Pair of string and int: (name, age)
- List of tuples: [(name, age)]
- Dictionary from strings to dates: {firstname: birthdate}
- Object with dynamic attributes: .name .age .phones=[phone]
- Putting it all together: {(country,city): [.name .phones=[phone]]}
- Example from the intro: {(name1,name2): .calls={phone:(start,finish)}, .topics=[topicid]}
10 September 2009
A notation for nested data structures in Python
The Python programming language makes it easy to compose collections into nested hierachies, built up from lists, dictionaries, tuples and objects with generated attributes. One can put together a dictionary that maps a pairs of strings to lists of objects whose "calls" attribute is a dictionary mapping ints to pairs of dates and whose "topics" attribute is a list of integers.
If for some reason such a nested data structure is actually returned by a function (e.g. to fill a complex report template), anyone using it will also need to understand what it holds. So, I've been refining a notation that I use in docstrings to concisely express the structure a composition of collections. Here it is by example:
0 comments:
Post a Comment