suppressing import errors
Arnaud Delobelle
arnodel at gmail.com
Tue Nov 15 16:42:22 EST 2011
On 15 November 2011 21:34, Chris Angelico <rosuav at gmail.com> wrote:
> On Wed, Nov 16, 2011 at 8:20 AM, David Riley <fraveydank at gmail.com> wrote:
>> Comparisons to singletons like None should always be done with
>> 'is' or 'is not', never the equality operators.
>>
>> Also, beware of writing "if x" when you really mean "if x is not None"
>> -- e.g. when testing whether a variable or argument that defaults to
>> None was set to some other value. The other value might have a type
>> (such as a container) that could be false in a boolean context!
>
> It's probably quicker to execute "if x is None" than "if x"
> (presumably the former just compares the two pointers). On the other
> hand, it's more compact to leave off the "is None". And on the
> gripping hand, neither "quicker to execute" nor "more compact" equates
> to "more Pythonic".
It's idiomatic to write "x is None" when you want to know whether x is None.
--
Arnaud
More information about the Python-list
mailing list