Hi, people. I just noticed this:
class booleen(bool): ... def __repr__(self): ... if self: ... return "Vrai" ... return "Faux" ... Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: type 'bool' is not an acceptable base type
and am a bit surprised that `bool' refuses to be sub-classed. Not that I cannot live without it, of course! But it might be useful being able to "cast" a `bool' into something for which I could control the representation, while keeping all other properties of `bool'. Is there a deep reason behind the forbidding? Or was it merely for possibly protecting users against themselves? :-) For me, this is a question, much more than an issue. Yet, if there is no deep reason, maybe the forbidding could be lifted? -- François Pinard http://www.iro.umontreal.ca/~pinard
On Thu, Feb 12, 2004, François Pinard wrote:
Hi, people. I just noticed this:
class booleen(bool): ... def __repr__(self): ... if self: ... return "Vrai" ... return "Faux" ... Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: type 'bool' is not an acceptable base type
Just to bolster Francois's case, I ran into this precise use case a week ago myself (where I wanted a bool but needed to change the output string). Didn't bother complaining about it, though. ;-) -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ "Argue for your limitations, and sure enough they're yours." --Richard Bach
On Feb 12, 2004, at 2:50 PM, Aahz wrote:
On Thu, Feb 12, 2004, François Pinard wrote:
Hi, people. I just noticed this:
class booleen(bool): ... def __repr__(self): ... if self: ... return "Vrai" ... return "Faux" ... Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: type 'bool' is not an acceptable base type
Just to bolster Francois's case, I ran into this precise use case a week ago myself (where I wanted a bool but needed to change the output string). Didn't bother complaining about it, though. ;-)
Shouldn't you be "adapting" the objects for display with some kind of value transformer.. instead of trying to change the objects themselves in the "model"? You could do this with something like PyProtocols, for example. You really have to do something like this in most cases anyway, because __repr__ gets coerced to str if you use the repr() function. -bob
On Feb 12, 2004, at 2:41 PM, François Pinard wrote:
Hi, people. I just noticed this:
class booleen(bool): ... def __repr__(self): ... if self: ... return "Vrai" ... return "Faux" ... Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: type 'bool' is not an acceptable base type
and am a bit surprised that `bool' refuses to be sub-classed. Not that I cannot live without it, of course! But it might be useful being able to "cast" a `bool' into something for which I could control the representation, while keeping all other properties of `bool'.
Is there a deep reason behind the forbidding? Or was it merely for possibly protecting users against themselves? :-) For me, this is a question, much more than an issue. Yet, if there is no deep reason, 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. You can subclass int and implement __nonzero__ just like bool does (in fact, bool does little more than that). 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! 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. -bob
[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
See http://mail.python.org/pipermail/python-dev/2002-March/020822.html [Guido] 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.
[Tim Peters]
See http://mail.python.org/pipermail/python-dev/2002-March/020822.html
Thanks for the reference!
[Guido] 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.)
This is a convincing argument, and enough a reason. Thanks! Now, I know why Python refuses! :-) -- François Pinard http://www.iro.umontreal.ca/~pinard
participants (4)
-
Aahz -
Bob Ippolito -
François Pinard -
Tim Peters