Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Last active July 15, 2020 23:45
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/c317d7c5df39e557f0e5c6e1e8f76ddd to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/c317d7c5df39e557f0e5c6e1e8f76ddd to your computer and use it in GitHub Desktop.
from collections import defaultdict
#default_factory is mentioned.
def func():
return NotAvailable
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 0x00827808>, {'a': 1, 'b': 2})
#Calling func raises NameError.
print (d.__missing__('a')) #Output:NameError: name 'NotAvailable' is not defined
print (d.__missing__('z'))#Output:NameError: name 'NotAvailable' is not defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment