[Tutor] Question about dictionary method get()
Dick Moores
rdm at rcblue.com
Mon Jun 28 11:42:42 EDT 2004
Francis Moore wrote at 06:47 6/28/2004:
>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'
Thanks very much again, Francis. Very clear, except for my not understanding
if __name__ == '__main__':
func()
But that's OK. I'll get to that later.
Dick Moores
More information about the Tutor
mailing list