Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python -- Class of functions [duplicate]

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.

like image 273
Nagesh Eranki Avatar asked Mar 16 '26 13:03

Nagesh Eranki


1 Answers

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.

like image 124
user2357112 supports Monica Avatar answered Mar 19 '26 03:03

user2357112 supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!