Pending: A Mixin class for testing

Peter Otten __peter__ at
Mon Nov 27 09:32:19 CET 2006

Scott David Daniels wrote:

> Here is a Mix-in class I just built for testing.
> It is quite simple, but illustrates how Mixins
> can be used.
>      class Pending(object):
>          _pending = iter(())
>          def __new__(class_, *args, **kwargs):
>              try:
>                  return
>              except StopIteration:
>                  return super(Pending, class_).__new__(class_,
>                                                 *args, **kwargs)
> Now for the use:
>      class Float(Pending, float): pass
>      Float._pending = iter(range(4,7))
>      print [Float(x*(x+1)//2) for x in range(6)]
> Possibly by using itertools functions such as chain as in:
>      Klass._pending = itertools.chain(generate_some(), Klass._pending)
> you can inject fixed values to simplify testing.

If you're willing to allow for issubclass() to fail you can do without a

from itertools import chain, repeat

def preload(class_, iterable):
    items = chain(
        (lambda *args, **kw: item for item in iterable), repeat(class_))
    return lambda *args, **kw:*args, **kw)

float = preload(float, "ABC")
print [float(x) for x in range(6)]
issubclass(float, float) # TypeError

Argh :-)


More information about the Python-list mailing list