除非我弄错了,在Python中创建一个函数是这样的:

def my_func(param1, param2):
    # stuff

但是,实际上并没有给出这些参数的类型。而且,如果我没记错的话,Python是一种强类型语言,因此,Python似乎不应该让你传入与函数创建者期望的不同类型的参数。然而,Python如何知道函数的用户正在传递正确的类型呢?假设函数实际使用了形参,如果它是错误的类型,程序会死吗?必须指定类型吗?


当前回答

您不需要指定类型。该方法只有在试图访问未在传入参数上定义的属性时才会失败(在运行时)。

这个简单的函数:

def no_op(param1, param2):
    pass

... 无论传入哪两个参数都不会失败。

然而,这个函数:

def call_quack(param1, param2):
    param1.quack()
    param2.quack()

... 如果param1和param2都没有名为quack的可调用属性,将在运行时失败。

其他回答

如果有人想指定变量类型,我已经实现了一个包装器。

import functools
    
def type_check(func):

    @functools.wraps(func)
    def check(*args, **kwargs):
        for i in range(len(args)):
            v = args[i]
            v_name = list(func.__annotations__.keys())[i]
            v_type = list(func.__annotations__.values())[i]
            error_msg = 'Variable `' + str(v_name) + '` should be type ('
            error_msg += str(v_type) + ') but instead is type (' + str(type(v)) + ')'
            if not isinstance(v, v_type):
                raise TypeError(error_msg)

        result = func(*args, **kwargs)
        v = result
        v_name = 'return'
        v_type = func.__annotations__['return']
        error_msg = 'Variable `' + str(v_name) + '` should be type ('
        error_msg += str(v_type) + ') but instead is type (' + str(type(v)) + ')'
        if not isinstance(v, v_type):
                raise TypeError(error_msg)
        return result

    return check

使用它作为:

@type_check
def test(name : str) -> float:
    return 3.0

@type_check
def test2(name : str) -> str:
    return 3.0

>> test('asd')
>> 3.0

>> test(42)
>> TypeError: Variable `name` should be type (<class 'str'>) but instead is type (<class 'int'>)

>> test2('asd')
>> TypeError: Variable `return` should be type (<class 'str'>) but instead is type (<class 'float'>)

EDIT

如果没有声明任何参数的(或返回值的)类型,上面的代码就不能工作。下面的编辑可以提供帮助,另一方面,它只对kwarg有效,不检查args。

def type_check(func):

    @functools.wraps(func)
    def check(*args, **kwargs):
        for name, value in kwargs.items():
            v = value
            v_name = name
            if name not in func.__annotations__:
                continue
                
            v_type = func.__annotations__[name]

            error_msg = 'Variable `' + str(v_name) + '` should be type ('
            error_msg += str(v_type) + ') but instead is type (' + str(type(v)) + ') '
            if not isinstance(v, v_type):
                raise TypeError(error_msg)

        result = func(*args, **kwargs)
        if 'return' in func.__annotations__:
            v = result
            v_name = 'return'
            v_type = func.__annotations__['return']
            error_msg = 'Variable `' + str(v_name) + '` should be type ('
            error_msg += str(v_type) + ') but instead is type (' + str(type(v)) + ')'
            if not isinstance(v, v_type):
                    raise TypeError(error_msg)
        return result

    return check

要有效地使用typing模块(Python 3.5新增),请包含all(*)。

from typing import *

你将准备使用:

List, Tuple, Set, Map - for list, tuple, set and map respectively.
Iterable - useful for generators.
Any - when it could be anything.
Union - when it could be anything within a specified set of types, as opposed to Any.
Optional - when it might be None. Shorthand for Union[T, None].
TypeVar - used with generics.
Callable - used primarily for functions, but could be used for other callables.

然而,你仍然可以使用类型名称,如int, list, dict,…

其他的回答很好地解释了鸭子的打字方式和tzot的简单回答:

Python没有变量,不像其他语言,变量有类型和值;它具有指向对象的名称,这些对象知道它们的类型。

然而,自2010年(第一次提出这个问题的时候)以来,有一件有趣的事情发生了变化,那就是PEP 3107的实现(在Python 3中实现)。现在你实际上可以像这样指定形参的类型和函数的返回类型:

def pick(l: list, index: int) -> int:
    return l[index]

这里我们可以看到pick有两个参数,一个列表l和一个整数索引。它还应该返回一个整数。

因此,这里暗示l是一个整数列表,我们可以很容易地看到,但对于更复杂的函数,列表应该包含什么可能有点令人困惑。我们还希望index的默认值为0。要解决这个问题,你可以选择这样写pick:

def pick(l: "list of ints", index: int = 0) -> int:
    return l[index]

请注意,我们现在放入一个字符串作为l类型,这在语法上是允许的,但它不适用于以编程方式解析(稍后我们将回到这一点)。

需要注意的是,如果你将浮点数传递给索引,Python不会引发TypeError,原因是Python设计哲学中的主要观点之一:“我们都是成年人”,这意味着你应该知道什么可以传递给函数,什么不能。如果你真的想编写抛出TypeErrors的代码,你可以使用isinstance函数来检查传入的参数是否属于正确的类型或它的子类,如下所示:

def pick(l: list, index: int = 0) -> int:
    if not isinstance(l, list):
        raise TypeError
    return l[index]

更多关于为什么你应该很少这样做,你应该做什么,在下一节和评论中讨论。

PEP 3107不仅提高了代码的可读性,而且还提供了几个合适的用例,您可以在这里阅读。


随着PEP 484的引入,类型注释在Python 3.5中得到了更多的关注,PEP 484引入了用于类型提示的标准模块类型。

这些类型提示来自类型检查器myypy (GitHub),它现在与PEP 484兼容。

typing模块提供了一个非常全面的类型提示集合,包括:

List, Tuple, Set, Dict -分别用于List, Tuple, Set和Dict。 Iterable -用于生成器。 什么都有可能。 Union——可以是指定类型集中的任何内容,而不是Any。 可选—当它可能为None时。联合的简写[T, None]。 TypeVar -与泛型一起使用。 可调用的-主要用于函数,但也可以用于其他可调用的。

这些是最常见的类型提示。在typing模块的文档中可以找到完整的清单。

下面是使用typing模块中引入的注释方法的旧示例:

from typing import List

def pick(l: List[int], index: int) -> int:
    return l[index]

一个强大的特性是Callable,它允许您键入注释以函数作为参数的方法。例如:

from typing import Callable, Any, Iterable

def imap(f: Callable[[Any], Any], l: Iterable[Any]) -> List[Any]:
    """An immediate version of map, don't pass it any infinite iterables!"""
    return list(map(f, l))

如果使用TypeVar而不是Any,上面的示例可能会变得更加精确,但这是留给读者的练习,因为我相信我的答案中已经包含了太多关于类型提示所支持的美妙新特性的信息。


以前,当使用Sphinx编写Python代码时,可以通过编写如下格式的文档字符串来获得上述一些功能:

def pick(l, index):
    """
    :param l: list of integers
    :type l: list
    :param index: index at which to pick an integer from *l*
    :type index: int
    :returns: integer at *index* in *l*
    :rtype: int
    """
    return l[index]

As you can see, this takes a number of extra lines (the exact number depends on how explicit you want to be and how you format your docstring). But it should now be clear to you how PEP 3107 provides an alternative that is in many (all?) ways superior. This is especially true in combination with PEP 484 which, as we have seen, provides a standard module that defines a syntax for these type hints/annotations that can be used in such a way that it is unambiguous and precise yet flexible, making for a powerful combination.

在我个人看来,这是Python有史以来最伟大的特性之一。我等不及人们开始利用它的力量了。抱歉回答这么长,但这就是我兴奋时发生的事情。


一个大量使用类型提示的Python代码示例可以在这里找到。

Python并不关心你将什么传递给它的函数。当您调用my_func(a,b)时,param1和param2变量将保存a和b的值。Python不知道您正在使用正确的类型调用函数,并期望程序员处理这一点。如果函数将使用不同类型的参数调用,您可以使用try/except块包装访问它们的代码,并以您想要的任何方式计算参数。

从静态或编译时类型检查的意义上讲,Python不是强类型的。

大多数Python代码都属于所谓的“Duck Typing”——例如,你寻找一个对象上读取的方法——你不关心对象是磁盘上的文件还是套接字,你只想从中读取N个字节。