-
-
Save IndhumathyChelliah/938ae235bb0ab8994c10f05359aa370a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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