Comparisons and sorting of a numeric class....
Steven D'Aprano
steve at pearwood.info
Mon Jan 12 23:57:20 EST 2015
On 2015年1月13日 09:35:43 +1100, Chris Angelico wrote:
> Subclassing bool breaks this invariant, unless you never instantiate the
> subclass, in which case it's completely useless.
Not necessarily. A class that you never instantiate, but use as an object
itself, is another way of implementing the singleton design pattern.
Obviously all the methods have to be class methods, but it is doable.
But wacky exceptions like subclasses that aren't instantiated aside,
normally if you subclass something you intend to instantiate it.
In Java terms bool is a "final" class which means it cannot be subclassed.
Although Python is not anywhere near as restrictive as Java, and makes a
virtue out of allowing the programmer to shot themselves in the foot,
there are some restrictions and this is one of them.
--
Steven
More information about the Python-list
mailing list