[Python-ideas] bool keyword: (was [Python-Dev] bool conversion wart?)

Ron Adam rrr at ronadam.com
Sat Mar 3 01:17:32 CET 2007

Josiah Carlson wrote:
> Ron Adam <rrr at ronadam.com> wrote:
>> Interpretation:
>> The "small" inconsistency (if any) here is the 'not' keyword vs the 
>> 'bool()' constructor. They pretty much do the same thing yet work in 
>> modestly different ways.
> Maybe I'm missing something, but is there a place where the following is
> true?
>     (not not x) != bool(x)
> I can't think of any that I've ever come across.
>  - Josiah

I don't think you are missing anything.  I did say it was a *small* 
inconsistency in how they are used in relation to 'and', 'or' and 'not'. 
ie.. a constructor vs a keyword in a similar situation.

And I also pointed out it doesn't change what they do, it has more to do 
with how they work underneath and that there may be some benefits in 
changing this.

 >>> def foo(x):
...   return (not not x) != bool(x)
 >>> dis.dis(foo)
   2           0 LOAD_FAST                0 (x)
               3 UNARY_NOT
               4 UNARY_NOT
               5 LOAD_GLOBAL              0 (bool)
               8 LOAD_FAST                0 (x)
              11 CALL_FUNCTION            1
              14 COMPARE_OP               3 (!=)
              17 RETURN_VALUE

In this case the lines...

               5 LOAD_GLOBAL              0 (bool)
               8 LOAD_FAST                0 (n)
              11 CALL_FUNCTION            1

Would be replaced by...

               5 LOAD_FAST                0 (n)
               6 UNARY_BOOL

The compiler could also replace UNARY_NOT, UNARY_NOT pairs with a single 

There may be other situations where this could result in different more 
efficient byte code as well.


More information about the Python-ideas mailing list