python参数检查类型_Python类型检查

python参数检查类型

1.什么是类型检查? (1. What is Type Checking?)

Type Checking is the programming language feature that specifies how the variables are created and their types are identified by the language compiler or interpreter.

类型检查是一种编程语言功能,它指定语言编译器或解释器如何创建变量以及如何标识变量的类型。

2.类型检查有哪些不同类型? (2. What are the different types of Type Checking?)

A programming language can be classified into the following based on type checking.

可以根据类型检查将编程语言分为以下几种。

  1. Statically Typed Languages – C, Java, C++, etc.

    静态类型语言 -C,Java,C ++等
  2. Dynamically Typed Languages – JavaScript, Python, Ruby, etc.

    动态类型语言 – JavaScript,Python,Ruby等

2.1)什么是静态类型检查? (2.1) What is Static Type Checking?)

The type of a variable is known at the compile-time. The type of a variable is fixed and we can’t change it at a later point of time.

变量的类型在编译时是已知的。 变量的类型是固定的,我们以后不能更改它。

Let’s look at the variable declaration in Java.

让我们看一下Java中的变量声明。

String str = "Hello";

If we try to change the type of the variable or assign a value of incompatible type, the compiler will throw an error.

如果我们尝试更改变量的类型或分配不兼容类型的值,则编译器将引发错误。

str = 10; // Type mismatch: cannot convert from int to String 

int str = 10; // Duplicate local variable str

2.2)什么是动态类型检查? (2.2) What is Dynamic Type Checking?)

The type of the variable is determined at the runtime. We don’t specify the type of the variable in the code. The code behaves differently based on the type of the object at runtime.

变量的类型在运行时确定。 我们没有在代码中指定变量的类型。 代码在运行时根据对象类型的不同而有所不同。

Let’s look at an example of a function definition in Python.

让我们看一下Python中的函数定义示例。

def add(x, y):
    return x + y


print(add(10, 5))
print(add('A', 'B'))

If the function arguments are integers, the sum is returned. If they are string, they are concatenated and returned.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值