[Tutor] get() method for dictionaries

Greg Whittier greg at thewhittiers.com
Mon Mar 15 19:47:01 EST 2004


I tend to use it only for the optional default argument 

d={}
d[1] = 'stuff'
d.get(1,"no stuff")
'stuff'
d.get(267,"no stuff")
'no stuff'

This is useful if you don't want to check for the key first or deal with
an exception.

Greg

On Mon, 2004-03-15 at 19:34, Christopher Spears wrote:
> Why do we need the get() method for dictionaries?  On
> the surface, there doesn't seem to be a whole of
> difference between:
> 
> d = {}
> d[1] = "stuff"
> d[1]
> 'stuff'
> 
> and
> 
> d.get(1)
> 'stuff'
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Greg Whittier <greg at thewhittiers.com>




More information about the Tutor mailing list