Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't extend bool in Python?

>>> class BOOL(bool):
...     print "why?"
... 
why?
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
    type 'bool' is not an acceptable base type

I thought Python trusted the programmer.

like image 806
Juanjo Conti Avatar asked Jan 31 '10 15:01

Juanjo Conti


People also ask

What does bool (' false ') return?

It returns False if the parameter or value passed is False.

What is bool error in Python?

The Python "TypeError: 'bool' object is not iterable" occurs when we try to iterate over a boolean value ( True or False ) instead of an iterable (e.g. a list). To solve the error, track down where the variable got assigned a boolean and correct the assignment.

How do you add a boolean in Python?

If you want to define a boolean in Python, you can simply assign a True or False value or even an expression that ultimately evaluates to one of these values. You can check the type of the variable by using the built-in type function in Python.

What happens if we call int () on a bool Python?

Integers and Floats as Booleans Integers and floating point numbers can be converted to the boolean data type using Python's bool() function. An int, float or complex number set to zero returns False .


5 Answers

Guido's take on it:

I thought about this last night, and realized that you shouldn't be allowed to subclass bool at all! A subclass would only be useful when it has instances, but the mere existance of an instance of a subclass of bool would break the invariant that True and False are the only instances of bool! (An instance of a subclass of C is also an instance of C.) I think it's important not to provide a backdoor to create additional bool instances, so I think bool should not be subclassable.

Reference: http://mail.python.org/pipermail/python-dev/2002-March/020822.html

like image 167
Max Shawabkeh Avatar answered Oct 16 '22 10:10

Max Shawabkeh


Since the OP mentions in a comment:

I want 1 and 2 to return an instance of my class.

I think it's important to point out that this is entirely impossible: Python does not let you alter built-in types (and, in particular, their special methods). Literal 1 will always be an instance of built-in type int, and in any case the basic semantics of the and operator are not overridable anyway -- a and b is always identical to b if a else a for any a and b (no bool coercion involved, even though the OP appears to mistakenly believe one is happening).

Restating this crucial point: the value of a and b is always, unchangeably either a or b -- there is no way to break this semantic constraint (even if a and b were instances of your own peculiar classes -- even less so of course when they're constrained to be instances of Python's built-in int!-).

like image 12
Alex Martelli Avatar answered Oct 16 '22 08:10

Alex Martelli


If you are using Python 3, and you want to have a class that can be evaluated as a boolean, but also contain other functionality, implement __bool__ in your class.

In Python 2 the same effect can be achieved by implementing __nonzero__ or __len__ (if your class is a container).

like image 10
AJ. Avatar answered Oct 16 '22 10:10

AJ.


Here is a post that explains the reasoning behind the decision: http://mail.python.org/pipermail/python-dev/2004-February/042537.html

The idea is that bool has a specific purpose - to be True or to be False, and adding to that would only serve to complicate your code elsewhere.

like image 9
danben Avatar answered Oct 16 '22 08:10

danben


Because bool is supposed to only have two values -- True and False. If you were able to subclass bool, you could define arbitrary numbers of values for it, and that's definitely not what you want to happen.

A better question is: why do you want to extend bool?

like image 3
John Feminella Avatar answered Oct 16 '22 10:10

John Feminella