[Python-3000] PEP 3100 Comments

Bill Janssen janssen at parc.com
Wed May 10 03:48:58 CEST 2006


> Currently in Python for a third party to add a mixin, they would 
> have to hack the class's __bases__, which I am not suggesting here at all.

I'm not suggesting it either.  Here's what I meant:

Consider the type "sequential_ordering", which has methods

	 len() => INTEGER
	 iter() => ITERABLE

Now let's say that you've got a type of your own, called "mytype", which defines

	 nitems() => INTEGER
	 iter() => ITERABLE

Using mixins, you'd do something like:

   class adapted_mytype (sequential_ordering, mytype):
	 def len(self):
	     return self.nitems()

You can now use "adapted_mytype", which respects both the
"sequential_ordering" protocol and the "mytype" protocol.  And a user
can use isinstance to test for that, avoiding "duck typing".

Bill


More information about the Python-3000 mailing list