Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8791959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:54:52+00:00 2026-06-13T22:54:52+00:00

To typecast a value to boolean, I usually do the following: not not value

  • 0

To typecast a value to boolean, I usually do the following:

not not value

This is faster than using bool. Output from timeit:

python -m timeit '[bool(t) for t in [[], {}, "", 0, [1], {"a": "n"}, "asdf", 2323]]'    
1000000 loops, best of 3: 1.81 usec per loop
python -m timeit '[(not not t) for t in [[], {}, "", 0, [1], {"a": "n"}, "asdf", 2323]]'
1000000 loops, best of 3: 1.11 usec per loop

I tried to test it using this:

>>> [bool(t) == (not not t) for t in [None, [], {}, "", 0, [1], {'a': 'n'}, "asdf", 2323]]
[True, True, True, True, True, True, True, True, True]

And it seems to work for most common cases.

Arguments on readability aside, where does this fail, or why would this be a bad thing to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:54:53+00:00Added an answer on June 13, 2026 at 10:54 pm

    As Ignacio notes, both bool() and not invoke the same method (see operator.not_, __nonzero__,and the note there about __len__), so there’s no need to compare them across different object types.

    You’ll get more accurate results if you test only the operator/function call (this is using ipython’s %timeit magic method):

    In [1]: %timeit not not 0
    10000000 loops, best of 3: 60.2 ns per loop
    
    In [2]: %timeit bool(1)
    1000000 loops, best of 3: 180 ns per loop
    
    In [3]: %timeit bool(0)
    1000000 loops, best of 3: 177 ns per loop
    
    In [4]: %timeit not not 1
    10000000 loops, best of 3: 60.5 ns per loop
    

    And Paulo’s suggestion:

    In [3]: %timeit True if 0 else False
    10000000 loops, best of 3: 73 ns per loop
    
    In [4]: %timeit True if 1 else False
    10000000 loops, best of 3: 54.4 ns per loop
    

    And one more, for funsies:

    In [6]: %timeit 0 and True or False
    10000000 loops, best of 3: 72.7 ns per loop
    
    In [7]: %timeit 1 and True or False
    10000000 loops, best of 3: 78.1 ns per loop
    

    (all these tests were run against Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53), [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin)

    So, to answer your question “would [using not not] be a bad thing to do”: yes, definitely. If you care about readability, bool(…) is clearer, and if you care about performance, True if … else False is faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Convert.ToInt32(ddlBuyer1Country.SelectedValue) ; to typecast a string returned by selectedvalue. But this
I was wondering what is the best way to typecast a value from one
This method that uses method-level generics, that parses the values from a custom POJO,
Hi all i need to typecast a String value into a char . Please
Division in sqlite return integer value sqlite> select totalUsers/totalBids from (select (select count(*) from
In the following code, I get an error when I typecast the float pointer
In VC++, when I typecast the const char* value returned by std::string::c_str() to char*
In MATLAB (r2009b) I have a uint32 variable containing the value 2147484101. This number
Background: I mostly run python scripts from the command line in pipelines and so
I have a record like this: Tcustomer=record Name: string; IDNumber: Integer; IsMarried: boolean; end;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.