简体   繁体   English

在Python中,函数“类型”的内置名称是什么?

[英]What is the builtin name of the 'type' of functions, in Python?

What Python builtin returns <type 'function'> ? 什么Python内置<type 'function'>返回<type 'function'> type'function <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? 有没有办法避免创建此lambda函数,以便获得一般的函数类型?

See http://www.finalcog.com/python-memoise-memoize-function-type for more details. 有关更多详细信息,请参见http://www.finalcog.com/python-memoise-memoize-function-type

Thanks, 谢谢,

Chris. 克里斯。

You should be able to use types.FunctionType to do what you want: 您应该能够使用types.FunctionType来执行types.FunctionType的操作:

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. 但是通常, def被认为是function类型的默认构造function

You should get away from the idea of 'types' in Python. 您应该摆脱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! 或者只是尝试调用它,然后捕获异常!

"What Python builtin returns <type 'function'> ?" “什么Python内置<type 'function'>返回<type 'function'> type'function <type 'function'> ?”

Functions. 职能。

"Is there way of avoiding creating this lambda function, in order to get the type of functions in general?" “是否有办法避免创建此lambda函数,以便获得一般的函数类型?”

Yes, types.FunctionType. 是的,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. 如果您要问如何摆脱lambda(但重读告诉我您可能不是),则可以定义一个函数而不是lambda。

So instead of: 所以代替:

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

You do 你做

>>> def thefunction(x):
...     return x+x
>>> somemethod(thefunction)

built-ins are not function s they are: builtin_function_or_method . 内置不是function它们是: 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'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM