Comparisons and sorting of a numeric class....

Chris Angelico rosuav at gmail.com
Mon Jan 12 21:11:12 EST 2015


On Tue, Jan 13, 2015 at 12:59 PM, Andrew Robinson
<andrew3 at r3dsolutions.com> wrote:
> There is no need to copy data from an initialized superclass instance into a
> subclass instance that has no new data, but only rebind -- or add a
> binding/proxy object -- to bind the superclass instance to the subclass
> methods.
>
> eg: what is now standard practice to create a new copy of the superclass:
>
> class myFalse( bool ): __new__( self, data ): return super( myFalse, self
> ).__new__(self,data)
>
> Could be replaced by a general purpose proxy meant to handle singleton
> subclassing:
>
> class myFalse( bool ):  __new__( self ): return
> bind_superinstance_to_subclass( False, myFalse )

I don't understand. What do you expect this to be doing? Are you
trying to replace the original False with your new subclass?

>> he Python bool type has the following
>> invariant, for any object x:
>>
>> assert not isinstance(x, bool) or x is True or x is False
> I mean, even right now -- with the language as-is -- let's define something
> that blatantly creates a new instance of something neither an actual
> instance of True nor False, and make that x -- and see if your assertion
> catches it:
>
> Python 2.7.5 (default, May 29 2013, 02:28:51)
> [GCC 4.8.0] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> x=(False,)
>>>> assert not isinstance(x, bool) or x is True or x is False
>>>>
>
> LOL ... no exception was raised... and we know if the assertion Failed, an
> exception ought to be raised:

The assertion did not fail. There are three parts, and as long as one
of them is true, the assertion will pass:

1) x isn't an instance of bool
2) x is the object known as True
3) x is the object known as False

You just gave an example of the first part of the invariant. That's an
instance of tuple, which is not a subclass of bool, ergo isinstance(x,
bool) returns False, negating that makes True, and the assertion
passes. [1]

> So -- your assertion, at least as shown, is pretty useless in helping
> determine why subclassing is not allowed, or instances of subclasses that
> are not distinct from their superclasses existing instance.

It exactly defines the nature of Python's bool type: there are
precisely two instances of it.

ChrisA

[1] Which puts me in mind of https://www.youtube.com/watch?v=D0yYwBzKAyY



More information about the Python-list mailing list