<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<br>
<blockquote cite="mid:54ABC52A.1050507@davea.name" type="cite">
<blockquote type="cite">So, I'm not sure I can subclass boolean
either because that too is a
<br>
built in class ... but I'm not sure how else to make an object
that
<br>
acts as boolean False, but can be differentiated from false by
the 'is'
<br>
operator. It's frustrating -- what good is subclassing, if one
cant
<br>
subclass all the base classes the language has?
<br>
</blockquote>
<br>
As I said above, make sure you have a constructor. If you still
get an error, post a message that shows exactly what you did, and
what exception you saw.
<br>
<br>
</blockquote>
<br>
OK.<br>
I tried to subclass bool, using __new__ just to see if it would even
accept the definition... eg: python 2.7.5<br>
<br>
>>> class UBool( bool ):<br>
... def __new__( self, default ): return bool.__new__( self,
default )<br>
... <br>
Traceback (most recent call last):<br>
File "<stdin>", line 1, in <module><br>
TypeError: Error when calling the metaclass bases<br>
type 'bool' is not an acceptable base type<br>
<br>
I also tried using return int.__new__( self, bool(default) ) but
that too failed the exact same way.<br>
<br>
<br>
I came across this in my searches, perhaps it has something to do
with why I can't do this?<br>
<a class="moz-txt-link-freetext" href="https://mail.python.org/pipermail/python-dev/2002-March/020822.html">https://mail.python.org/pipermail/python-dev/2002-March/020822.html</a><br>
<br>
<pre><font color="#ff0000">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.
</font></pre>
<font color="#ff0000">...<br>
</font>
<pre><font color="#ff0000">--Guido van Rossum</font></pre>
So, I think Guido may have done something so that there are only two
instances of bool, ever.<br>
eg: False and True, which aren't truly singletons -- but follow the
singleton restrictive idea of making a single instance of an object
do the work for everyone; eg: of False being the only instance of
bool returning False, and True being the only instance of bool
returning True.<br>
<br>
Why this is so important to Guido, I don't know ... but it's making
it VERY difficult to add named aliases of False which will still be
detected as False and type-checkable as a bool. If my objects don't
type check right -- they will likely break some people's legacy
code... and I really don't even care to create a new instance of
the bool object in memory which is what Guido seems worried about,
rather I'm really only after the ability to detect the subclass
wrapper name as distinct from bool False or bool True with the 'is'
operator. If there were a way to get the typecheck to match, I
wouldn't mind making a totally separate class which returned the
False instance; eg: something like an example I modified from
searching on the web:<br>
<br>
class UBool():<br>
def __nonzero__(self): return self.default<br>
def __init__( self, default=False ): self.default =
bool(default)<br>
def default( self, default=False ): self.defualt = bool(default)<br>
<br>
but, saying:<br>
<font color="#663300">>>> error=UBool(False)<br>
>>> if error is False: print "type and value match"<br>
...<br>
</font><font color="#663300">>>><br>
<br>
</font>Failed to have type and value match, and suggests that 'is'
tests the type before expanding the value.<br>
It's rather non intuitive, and will break code -- for clearly error
expands to 'False' when evaluated without comparison functions like
==.<br>
<br>
<font color="#663300">>>> if not error: print "yes it is
false"<br>
... <br>
yes it is false</font><br>
<font color="#663300">>>> print error.__nonzero__()<br>
False<br>
>>> if error==False: print "It compares to False properly"<br>
...<br>
>>><br>
</font><br>
So, both 'is' and == seems to compare the type before attempting to
expand the value.<br>
As a simple cross check, I tried to make a one valued tuple.<br>
<br>
<font color="#663300">>>> a=(False,None)<br>
>>> print a<br>
(False, None)<br>
>>> a=(False,)<br>
>>> if a is False: print "yes!!!"<br>
... <br>
>>></font><br>
<font color="#663300">>>> if not a: print "a is False"<br>
... <br>
>>> if a == False: print "a is False"<br>
</font><br>
but that obviously failed, too; and if == fails to say False==False
... well, it's just to sensitive for wrapper classes to be involved
unless they are a subclass of bool...<br>
<br>
Any ideas ?<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</body>
</html>