views:

294

answers:

5
>>> 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.

+7  A: 

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.

danben
+3  A: 

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?

John Feminella
I'd like to add an attribute that let me track some things on the object.
Juanjo Conti
@Juanjo then what you want is not a boolean, it is a tuple of a boolean and something else. A boolean must be either a True or a False, no other values and no other attributes that would make an instance of that class to compare negatively with both True and False. It that was to happen, it wouldn't be a boolean anymore.
Juliano
All right, but I can't override int.__nonzero__ to return this tuple. Can I?
Juanjo Conti
+11  A: 

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

Max Shawabkeh
True and False are singleton. This is a better answer.
Juanjo Conti
+2  A: 

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.

AJ
In Python 2.x you can do the same by implementing `__nonzero__` or `__len__`.
Max Shawabkeh
I want 1 and 2 to return an instance of my class. How would you do it?
Juanjo Conti
First subclassing bool, then overriding int methods... why don't you just make your own class from scratch?
Agos
+2  A: 

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!-).

Alex Martelli
@Alex great insight, thanks
AJ