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


Groups > comp.lang.python > #83652 > unrolled thread

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

Started byChris Angelico <rosuav@gmail.com>
First post2015-01-13 13:11 +1100
Last post2015-01-13 13:11 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#83652 — Re: Comparisons and sorting of a numeric class....

FromChris Angelico <rosuav@gmail.com>
Date2015-01-13 13:11 +1100
SubjectRe: Comparisons and sorting of a numeric class....
Message-ID<mailman.17651.1421115074.18130.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web