tags:

views:

244

answers:

4

What Python builtin returns <type 'function'>?

>>> type(lambda: None)
<type 'function'>

Is there way of avoiding creating this lambda function, in order to get the type of functions in general?

See http://www.finalcog.com/python-memoise-memoize-function-type for more details.

Thanks,

Chris.

A: 

built-ins are not functions they are: builtin_function_or_method. Isn't it the whole point of naming?

you can get by doing something like:

>>> type(len)
<class 'builtin_function_or_method'>
SilentGhost
This is correct, but it doesn't answer the question.
Daniel Pryden
+4  A: 

You should be able to use types.FunctionType to do what you want:

    Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51) 
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import types
    >>> help(types.FunctionType)

    Help on class function in module __builtin__:

    class function(object)
     |  function(code, globals[, name[, argdefs[, closure]]])
     |  
     |  Create a function object from a code object and a dictionary.
     |  The optional name string overrides the name from the code object.
     |  The optional argdefs tuple specifies the default argument values.
     |  The optional closure tuple supplies the bindings for free variables.

But generally, def is considered the default constructor for the function type.

Daniel Pryden
This is the correct answer - hopefuly you get accepted.
jkp
It's the 'types.FunctionType' answer that I was looking for. Thanks for the info.
chrisdew
+1  A: 

"What Python builtin returns <type 'function'>?"

Functions.

"Is there way of avoiding creating this lambda function, in order to get the type of functions in general?"

Yes, types.FunctionType. or just type(anyfunction)

If you are asking how to get rid of lambdas (but a reread tells me you probably are not) you can if define a function instead of the lambda.

So instead of:

>>> somemethod(lambda x: x+x)

You do

>>> def thefunction(x):
...     return x+x
>>> somemethod(thefunction)
Lennart Regebro
+2  A: 

You should get away from the idea of 'types' in Python. Most of the time you don't want to check the 'type' of something. Explicitly checking types is prone to breakage, for example:

>>> s1 = 'hello'
>>> s2 = u'hello'
>>> type(s1) == type(s2)
False

What you want to do is check if the object supports whatever operation you're trying to perform on it.

If you want to see if a given object is a function, do this:

>>> func = lambda x: x*2
>>> something_else = 'not callable'
>>> callable(func)
True
>>> callable(something_else)
False

Or just try calling it, and catch the exception!

lost-theory
In the spirit of duck typing, you can also use `hasattr(func, "func_code")` in Python < 3.0, or `hasattr(func, "__code__")` in Python >= 3.0. However, the `function` class is special enough that I think checking for it by type makes sense.
Daniel Pryden
Also, note that there is a difference between the `function` type and callables, since an instance of any class may be callable. I can't understand exactly what the OP's code is trying to do to understand if a callable would be sufficient for his purposes.
Daniel Pryden