Python - function class

def Func():
    pass

      

In an IPython session, when you type (Func), the output is a "function". Since Python is an object oriented language, I am assuming there is a class that "Func" is an instance of. So I tried to create my own class like this:

class MyFunc(function):
    pass

      

Alas, the interpreter just threw a "NameError", complaining that the "function" was undefined. So, I tried to be a little smart and did this:

class myfunc(func.__class__):
    pass

      

Alas, this failed miserably ...

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

      

In general, someone can explain: 1). How to create a class derived from a "function" type. If this is not possible, why? 2). This message really meant.

+3


source to share


1 answer


In general, someone can explain: 1). How to create a class derived from a "function" type.

You can not.



If this is not possible, why? 2). This message really meant.

This error message means that Python will not allow classes to 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 expand them.

0


source







All Articles