3
def Func():
    pass

In an IPython session, when I enter type(Func) the output is "function". As Python is an object oriented language, I assume there is a class of which "Func" is an instance. So I tried creating my own class this way:

class MyFunc(function):
    pass

Alas, the interpreter just threw a "NameError" complaining that "function" is not defined. So, I tried being a little clever and did this:

class myfunc(func.__class__):
    pass

Alas, it failed miserably...

TypeError: Error when calling the metaclass bases
    type 'function' is not an acceptable base type

In summary, could anyone explain: 1). How to create a class derived off of the type "function". If not possible, why? 2). What that error message really meant.

Nagesh Eranki
  • 125
  • 1
  • 9

1 Answers1

1

In summary, could anyone explain: 1). How to create a class derived off of the type "function".

You can't.

If not possible, why? 2). What that error message really meant.

That error message means that Python won't let classes use the function type as a base class. Classes implemented in C can only be extended if they set a specific flag; if they don't, this is the error you get when you try to extend them.

user2357112
  • 260,549
  • 28
  • 431
  • 505