Python args:def func(value:str)后面的两个点和一个类是什么意思

Python args:def func(value:str)后面的两个点和一个类是什么意思,python,parameter-passing,Python,Parameter Passing,我不明白arg:str的意思 def hello(arg: str): print(type(arg)) print('do stuff!', arg) hello('something') 我以为它指定了一个类型,但我试着打电话给hello,没有任何异常。我试着用int呼叫hello,没有错误 我已经做了一些测试,但我还没有想出什么 你知道这是怎么回事吗 Matt您是正确的def hello(arg:str):没有为参数指定类型。但在Python中,它只提供类型暗示。这意

我不明白arg:str的意思

def hello(arg: str):
    print(type(arg))
    print('do stuff!', arg)


hello('something')
我以为它指定了一个类型,但我试着打电话给hello,没有任何异常。我试着用
int
呼叫
hello
,没有错误

我已经做了一些测试,但我还没有想出什么

你知道这是怎么回事吗


Matt

您是正确的
def hello(arg:str):
没有为参数指定类型。但在Python中,它只提供类型暗示。这意味着理论上可以将任何类型的实例传递给函数。类型暗示对于开发和IDE很有用,因为它可以帮助程序在向函数传递意外参数时通知您

额外注意,这些类型提示存储在函数中的
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuu注释下:

In [1]: def f(arg: int):
   ...:     return arg
   ...: 

In [2]: f.__annotations__
Out[2]: {'arg': int}

@ŁukaszRogalski这可能是一个不同的问题,因为它专门询问参数类型暗示。因此,这只是为了帮助开发人员,python不会在乎它是否是正确的类型…@math2001。您的程序将在有或无类型提示的情况下执行。