4

Since everything is an object in Python (even functions) is it possible to actually extend from function class? For example I can do the following with string type. Is there any way to do something similar on function type?

class Foo(str):
    def appendFoo(self):
        return self+'foo'

foo = Foo('test')
print foo
>>> test
print foo.upper()
>>> TEST
print foo.appendFoo()
>>> testfoo
marcin_koss
  • 5,585
  • 8
  • 43
  • 61

1 Answers1

2

Nope, I wish. You are stuck with functors.

>>> def f(): pass
...
>>> type(f)
<type 'function'>
>>> function
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined
>>> function = type(f)
>>> class MyFunc(function):
...     pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
    type 'function' is not an acceptable base type
>>> type('MyFunc', (function,), {})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type 'function' is not an acceptable base type
pyrospade
  • 7,442
  • 2
  • 33
  • 51