function operators

Delaney, Timothy tdelaney at avaya.com
Mon Nov 26 22:22:25 EST 2001


> From: Terry Reedy [mailto:tjreedy at home.com]
> "Hans Nowak" <wurmy at earthlink.net> wrote in message
>
> Does 2.2 allow one to inherit from type 'function'?  So one could do
> this without extra level on indirection?

# 2.2b1

from __future__ import nested_scopes

class ComposableFunction(function):

    def __init__(self, f):
        self.f = f

    def __call__(self, *args, **kwargs):
        return apply(self.f, args, kwargs)

    def __add__(self, other):
        assert isinstance(other, ComposableFunction)
        def glue(*args):
            return self.f(*args) + other.f(*args)
        return ComposableFunction(glue)

NameError: name 'function' is not defined

from __future__ import nested_scopes
from types import FunctionType as function

class ComposableFunction(function):

    def __init__(self, f):
        self.f = f

    def __call__(self, *args, **kwargs):
        return apply(self.f, args, kwargs)

    def __add__(self, other):
        assert isinstance(other, ComposableFunction)
        def glue(*args):
            return self.f(*args) + other.f(*args)
        return ComposableFunction(glue)

TypeError: type 'function' is not an acceptable base type

So, no.

Whether it should or not is another question of course ... :)

Tim Delaney




More information about the Python-list mailing list