Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python, is function an object?

Given the following behaviour:

    def a():
       pass


    type(a)
    >> function

If type of a is function, what is type of function?

    type(function)
    >> NameError: name 'function' is not defined

And why does type of type from a is type?

    type(type(a))
    >> type

Lastly: if a is an object, why it cannot be inherited?

    isinstance(a, object)
    >> True

    class x(a):
       pass
    TypeError: Error when calling the metaclass bases
        function() argument 1 must be code, not str
like image 384
Jonathan Simon Prates Avatar asked Jul 11 '14 21:07

Jonathan Simon Prates


1 Answers

The type of any function is <type 'function'>. The type of the type of function is <type 'type'>, like you got with type(type(a)). The reason type(function) doesn't work is because type(function) is trying to get the type of an undeclared variable called function, not the type of an actual function (i.e. function is not a keyword).

You are getting the metaclass error during your class definition because a is of type function and you can't subclass functions in Python.

Plenty of good info in the docs.

like image 169
Alex W Avatar answered Oct 16 '22 00:10

Alex W