Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83652

Re: Comparisons and sorting of a numeric class....

References (6 earlier) <54ACAA04.60801@r3dsolutions.com> <54ADC99F.3020405@stoneleaf.us> <54B44A64.7010105@r3dsolutions.com> <CAPTjJmr0Bqcyj=VBADVSthSZ+pYMXQeNC-j8qLC2xHAVLUmcgA@mail.gmail.com> <54B47C0E.208@r3dsolutions.com>
Date 2015-01-13 13:11 +1100
Subject Re: Comparisons and sorting of a numeric class....
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.17651.1421115074.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Jan 13, 2015 at 12:59 PM, Andrew Robinson
<andrew3@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-13 13:11 +1100

csiph-web