I want try:
"%(name)s, %(age)s" % {"name":'Bob'}`
and console Bob, 20
or Bob,
.
But, this'll raise:
Traceback (most recent call last):`
File "<pyshell#4>", line 1, in <module>`
"%(name)s, %(age)s" % {"name":'a'}`
KeyError: 'age'`
How do I set default value?
In fact, I want get Bob,
when I input "%(name)s, %(age)s" % {"name":'Bob'}
don't raise Except.
You can subclass
dict
this way:# Usage:
Both of lines above prints
Bob,
See it working online
See version of this code written in Python 3 working online
Edit
I reinvited
collections.defaultdict
.See it working online