[Python-Dev] type categories
Oren Tirosh
oren-py-d@hishome.net
Sat, 24 Aug 2002 10:44:36 -0400
On Sat, Aug 24, 2002 at 02:44:27AM -0400, Guido van Rossum wrote:
> Why do keep arguing for inheritance? (a) the need to deny inheritance
> from an interface, while essential, is relatively rare IMO, and in
> *most* cases the inheritance rules work just fine; (b) having two
> separate but similar mechanisms makes the language larger.
Inheriting the implementation without implementing the same interfaces
is only one reason to want an interface mechanism that is not 100%
tied to inheritance. Objects written by different authors on two sides
of the globe often implement the same protocol without actually inheriting
the definition from a common module. I can pass these objects to a method
expecting this protocol and it will work just fine (most of the time...)
I would like to be able to declare that I need an object with a specific
interface even if the object was written long before and I don't want to
modify an existing library just to make it conform to my interface names.
Strictly defined named interfaces like Zope interfaces are also important
but most of the interfaces I use in everyday programming are more ad-hoc
in nature and are often defined retroactively.
> For example, if we ever are going to add argument type declarations to
> Python, it will probably look like this:
>
> def foo(a: classA, b: classB):
> ...body...
>
> It would be convenient if this could be *defined* as
>
> assert isinstance(a, classA) and isinstance(b, classB)
In your Optional Static Typing presentation slides you define "type
expressions". If the isinstance method accepted a type expression
object as its second argument this assertion would work for interfaces
that are not defined by strict hierarchical inheritance.
> so that programs that have a simple class hierarchy can use their
> classes directly as argument types, without having to go through the
> trouble of declaring a parallel set of interfaces.
...and classes could be used too. They are just type expressions that
match a single class.
BTW, isinstance already supports a simple form of this: a tuple is
interpreted as an "OR" type expression. You can say that isinstance
returns True if the object is an instance of one of the types matched
by the type expression.
Oren