I’ve just read PEP-483 and I found this:
Subtype relationships
(…)
By this definition:
- (…)
- The set of values becomes smaller in the process of subtyping, while the set of functions becomes larger.
It seems like if the author(s) thought that if we do class Derive(Base):
then the numbers of all possible instances of Derive
is smaller than Base
. Later we read:
An intuitive example: Every
Dog
is anAnimal
, alsoDog
has more functions, for example it can bark, thereforeDog
is a subtype ofAnimal
.
Well…it is clear that dogs are a kind of animal, so the set of all dogs is smaller than the set of all animals. But this doesn’t work the same with a computer’s objects.
Consider this:
class Vector2D:
x: int
y: int
class Vector3D(Vector2D):
z: int
According to the “the set of functions becomes larger” part of the sentence, Vector3D
has more functions than Vector2D
: it can get/set the z
field (this can be considered a ‘function’ of sort…). HOWEVER the set of all possible values of Vector3D
is bigger than the set of Vector2D
values: 2D vector has got all_values_of_int ^ 2
while 3D vector has all_values_of_int ^ 3
. It thus violates the “The set of values becomes smaller” part.
Therefore, according of that way of thought Vector3D
is not a subtype of Vector2D
, which is not only counter-intuitive but it is impossible to find any static type checker that would say the same.
Am I right?