[Tutor] Question about dictionary method get()

Francis Moore Francis.Moore at shaws.co.uk
Mon Jun 28 09:47:43 EDT 2004


From: Dick Moores [mailto:rdm at rcblue.com] 

> How do I "check for or propagate" None?

Checking for 'None' is as follows:

def func():
	d = {}
	d['a'] = 1
	d['b'] = 2
	d['c'] = 3

	if d.get('d') == None:
		print 'Key of "d" not found'
	
if __name__ == '__main__':
	func()

This will print 'Key of "d" not found'

I don't have my copy of Python In A Nutshell to hand to check the 
index for propogation of a value but I'm assuming that it means
to raise an exception. As the default implementation of the 
dictionary class does not raise an exception when a key is not 
found, you can raise your own. Like so:

	if d.get('d') == None:
		raise Exception, 'Key of "d" not found'

Someone more knowledgeable on the list might want to clarify that 
my general understanding of propagating 'None' is correct.

> Sorry for the basic questions.

No problem.

Cheers,
Francis. 
  
CONFIDENTIALITY NOTICE 
This communication contains information which is confidential and may also be privileged. It is for the exclusive use of the intended recipient(s). If you are not the intended recipient(s) please note that any distribution, copying or use of this communication or the information in it is strictly prohibited. If you have received this communication in error please notify us by e-mail or by telephone (+44(0) 1322 621100) and then delete the e-mail and any copies of it.  
This communication is from Shaw & Sons Limited whose registered office is at Shaway House, 21 Bourne Park, Bourne Road, Crayford, Kent DA1 4BZ. The views expressed in this communication may not be the views held by Shaw & Sons Limited. 
This message has been checked for all known viruses by McAfee VirusScan. 
 



More information about the Tutor mailing list