[Python-Dev] bool does not want to be subclassed? (original) (raw)

François Pinard pinard at iro.umontreal.ca
Thu Feb 12 16:01:32 EST 2004


[Bob Ippolito]

On Feb 12, 2004, at 2:41 PM, François Pinard wrote:

>[...] and am a bit surprised that `bool' refuses to be sub-classed. >Is there a deep reason behind the forbidding? Yet, [if not], maybe >the forbidding could be lifted?

Probably because bool is supposed to only have two values, True or False.. not True, False, Yes, No, Vrai, Faux.

I do not understand the argument. int' is supposed to only have int values (0, 1, -1, 2, -2, etc.), and we may subclass it nevertheless. Are you quoting "Yes", "No", "Vrai" and "Faux" as other names for True and False, or for other intermediate values between True and False? Once again, I do not understand the argument, nor the relation with the restriction of bool' not being subclass-able.

You can subclass int and implement nonzero just like bool does (in fact, bool does little more than that).

There is undoubtedly a lot of things I could do. I was just curious about the deep reasons why Python does not allow this particular thing.

Is there a good reason for wanting to do this? Do you want to "translate" all of Python to French? This is certainly not the way to go about it!

"all of Python to French"? As you go with hyperbole! :-)

No. This is for one rather big Python program I did not even write, but which I may soon have to maintain for a while. There is a lot of cruft in that program (in my opinion) coming from all the handling of subtle nuances between truth and falsity, which merely show in the printouts. I am merely pondering the hypothesis that if I could "attach" types to booleans as they get computed, it could simplify the program and ease maintenance somehow. The simplest way to tag a bool' might be to subclass it. The type could drive how the bool' gets printed, but also hints the algorithm about where/why it was produced.

OK, we could argue a lot about if I should proceed like this or otherwise. This is not much important in practice, and not the issue here. Normally, Python lets me do choose my ways without interfering, and this is no problem in practice because its reasons are almost always very clear to me. But here, Python tells me "Don't do that!", and I wonder why it chooses to interfere in this case.

You're going to receive True and False from other functions/keywords whether you like it or not. I would bet that "not Vrai" would be "False", for example.

"Whether I like it or not". When my girlfriend use that expression, not often hopefully, I usually read her as irritated :-). I do not feel aggressed by Python, and there is no fight in here. We are strictly speaking about overriding the repr (and maybe str), and also overriding the type with a sub-class in case this useful.

-- François Pinard http://www.iro.umontreal.ca/~pinard



More information about the Python-Dev mailing list