Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Last active July 15, 2020 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/938ae235bb0ab8994c10f05359aa370a to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/938ae235bb0ab8994c10f05359aa370a to your computer and use it in GitHub Desktop.
from collections import defaultdict
# default_factory is specified.
def func():
return "Not available"
d = defaultdict(func)
d['a'] = 1
d['b'] = 2
# default_factory attribute is given as user_defined function.
print(d) # Output:defaultdict(<function func at 0x009B7808>, {'a': 1, 'b': 2})
# Returns the default value of key which is "Not available"
print(d.__missing__('a')) # Output:Not available
print(d.__missing__('z')) # Output:Not available
#'z' is inserted to defaultdict
print (d)#Output:defaultdict(<function func at 0x01537808>, {'a': 'Not available', 'b': 2, 'z': 'Not available'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment