Extending the 'function' built-in class

Tim Chase python.list at tim.thechases.com
Sun Dec 1 14:43:59 EST 2013


On 2013-12-01 19:18, G. wrote:
> Hi, I can't figure out how I can extend the 'function' built-in
> class. I tried: class test(function):
>     def test(self):
>       print("test")
> but I get an error. Is it possible ?

While I don't have an answer, I did find this interesting.  First,
"function" doesn't seem to be in the default __buitin__ namespace:

  >>> function
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  NameError: name 'function' is not defined

I presume you're doing it with the following:

  >>> from types import FunctionType
  >>> class MyFunc(FunctionType):
  ...     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

but, as you mention, the inability to subclass it is somewhat
peculiar.  It appears to be metaclass-related.

I'm not quite sure *why* one might want to subclass FunctionType, but
I'm also not sure why you should be *prevented* from subclassing it.

-tkc





More information about the Python-list mailing list