Python Language Format using Getitem and Getattr


Example

Any data structure that supports __getitem__ can have their nested structure formatted:

person = {'first': 'Arthur', 'last': 'Dent'} 
'{p[first]} {p[last]}'.format(p=person) 
# 'Arthur Dent'

Object attributes can be accessed using getattr():

class Person(object):
    first = 'Zaphod'
    last = 'Beeblebrox'

'{p.first} {p.last}'.format(p=Person())
# 'Zaphod Beeblebrox'