[Tutor] if ... else shorthand form ?
Tiago Saboga
tiagosaboga at terra.com.br
Sat Feb 9 12:02:55 CET 2008
On Sat, Feb 09, 2008 at 09:21:41AM +0000, dave selby wrote:
> Hi all,
>
> Returning to python after a brief affair with C++, I have the following code ...
>
> if (items.has_keys('snapshot_interval')):
> self.snap_init[i] = items['snapshot_interval']
> else:
> self.snap_init[i] = 0
>
> I thought Python had a shorthand version of something like ....
>
> self.snap_init[i] =
> (items.has_keys('snapshot_interval'))?items['snapshot_interval']:0
self.snap_init[1] = items.get('snapshot_interval', 0)
Or, if you also want to set the value on items,
... = items.setdefault('snapshot_interval', 0)
[]s
Tiago.
More information about the Tutor
mailing list