【发布时间】:2021-06-11 08:23:42
【问题描述】:
这是一件事 我想在 python 中预定义函数接受的任何参数的 DataType,就像我们在 C 或 C++ 中所做的那样。
# For Example
def maw(x, y):
return x+y # I want to predefine this x and y to be integer-type
就像
// This is C-Program which accepts only integer as a parameter
int main(int x, int y){
return x+y;
}
我不想用另一行代码过滤这个参数,比如
if 'int' in type(x):
pass
else:
return False
【问题讨论】:
-
类似
assert(isinstance(x, int) and isinstance(y, int))? -
类型注释不会引发错误,它们只是为了方便
标签: python function filter arguments