Python 函数参数dtype声明不起作用?
为什么这个不能返回“12”? “+”符号应该连接两个字符串,而不是添加它们Python 函数参数dtype声明不起作用?,python,python-3.x,function,annotations,Python,Python 3.x,Function,Annotations,为什么这个不能返回“12”? “+”符号应该连接两个字符串,而不是添加它们 def foo(a:str, b:str): print(a+b) foo(1,2) 3 这不是注释的用途。注释是元数据,不是Python转换数据的指令 从: 参数名称后面可能有格式为“:expression”的注释。任何参数都可能有注释,即使是格式为*标识符或**标识符的参数。函数可以在参数列表后有“->表达式”形式的“return”注释。这些注释可以是任何有效的Python表达式,并在执行函数定义时进行计算
def foo(a:str, b:str):
print(a+b)
foo(1,2)
3
这不是注释的用途。注释是元数据,不是Python转换数据的指令 从: 参数名称后面可能有格式为“:expression”的注释。任何参数都可能有注释,即使是格式为*标识符或**标识符的参数。函数可以在参数列表后有“->表达式”形式的“return”注释。这些注释可以是任何有效的Python表达式,并在执行函数定义时进行计算。注释的计算顺序可能与它们在源代码中出现的顺序不同。注释的存在不会改变函数的语义 勇敢的皇帝是我的 例如,使用注释将类型信息附加到用于静态分析的函数,验证代码是否实际传入了预期传入的类型 只需显式地转换您的值;在通话中:
foo(str(1), str(2))
或者在函数本身中:
def foo(a, b):
print(str(a) + str(b))
或者在一个装饰师:
import functools
import inspect
def typeconversion(f):
"""Converts arguments with a callable attached in the parameter annotation"""
sig = inspect.signature(f)
@functools.wraps(f)
def wrapper(*args, **kwargs):
# convert any argument (including defaults), for which there is a
# callable annotation
bound = sig.bind(*args, **kwargs)
bound.apply_defaults()
args = bound.arguments
for param in sig.parameters.values():
if param.annotation is not param.empty and callable(param.annotation):
args[param.name] = param.annotation(args[param.name])
# call the function with the converted arguments
result = f(*bound.args, **bound.kwargs)
# convert the return value
if sig.return_annotation is not sig.empty and callable(sig.return_annotation):
result = sig.return_annotation(result)
return result
return wrapper
演示:
这不是注释的用途。注释是元数据,不是Python转换数据的指令 从: 参数名称后面可能有格式为“:expression”的注释。任何参数都可能有注释,即使是格式为*标识符或**标识符的参数。函数可以在参数列表后有“->表达式”形式的“return”注释。这些注释可以是任何有效的Python表达式,并在执行函数定义时进行计算。注释的计算顺序可能与它们在源代码中出现的顺序不同。注释的存在不会改变函数的语义 勇敢的皇帝是我的 例如,使用注释将类型信息附加到用于静态分析的函数,验证代码是否实际传入了预期传入的类型 只需显式地转换您的值;在通话中:
foo(str(1), str(2))
或者在函数本身中:
def foo(a, b):
print(str(a) + str(b))
或者在一个装饰师:
import functools
import inspect
def typeconversion(f):
"""Converts arguments with a callable attached in the parameter annotation"""
sig = inspect.signature(f)
@functools.wraps(f)
def wrapper(*args, **kwargs):
# convert any argument (including defaults), for which there is a
# callable annotation
bound = sig.bind(*args, **kwargs)
bound.apply_defaults()
args = bound.arguments
for param in sig.parameters.values():
if param.annotation is not param.empty and callable(param.annotation):
args[param.name] = param.annotation(args[param.name])
# call the function with the converted arguments
result = f(*bound.args, **bound.kwargs)
# convert the return value
if sig.return_annotation is not sig.empty and callable(sig.return_annotation):
result = sig.return_annotation(result)
return result
return wrapper
演示:
这里的dtype是什么?我以为它是将类型声明为“str”,但它似乎只是元数据。即使它是python没有的类型删除,您难道不认为它会出错,因为您传递了int对象吗?python中的dtype通常指、numpy数组或类似集合中元素的数据类型。它不是指静态类型注释或诸如此类的东西。这里的数据类型是什么?我以为它将类型声明为“str”,但它似乎只是元数据。即使它是python没有的类型删除,在传递int对象之后,您不认为它会出错吗?Python中的dtype通常表示、numpy数组或类似集合的元素的数据类型。它并不意味着静态类型注释或类似的东西。