Python 简明教程

Python - Dynamic Typing

一个突出的 features of Python language 是它是一种动态类型的语言。基于编译器的语言 C/C++、Java 等是静态类型的。让我们试着了解静态类型和动态类型之间的差异。

One of the standout features of Python language is that it is a dynamically typed language. The compiler-based languages C/C++, Java, etc. are statically typed. Let us try to understand the difference between static typing and dynamic typing.

在静态类型语言中,每个变量及其数据类型在给它赋值之前必须声明。任何其他类型的值都不能被编译器接受,并且会引发编译时错误。

In a statically typed language, each variable and its data type must be declared before assigning it a value. Any other type of value is not acceptable to the compiler, and it raises a compile-time error.

我们来看一个 Java 程序的以下代码段:

Let us take the following snippet of a Java program −

public class MyClass {
   public static void main(String args[]) {
      int var;
      var="Hello";

      System.out.println("Value of var = " + var);
   }
}

这里, var 被声明为一个整数变量。当我们尝试给它赋予一个字符串值时,编译器给出了以下错误消息:

Here, var is declared as an integer variable. When we try to assign it a string value, the compiler gives the following error message −

/MyClass.java:4: error: incompatible types: String cannot be converted to int
   x="Hello";
     ^
1 error

Why Python is Called Dynamically Typed?

variable in Python 只是一个标签或对存储在内存中的对象的引用,而不是命名的内存位置。因此,不需要事先声明类型。因为它只是一个标签,所以可以将其放在另一个对象上,该对象可以是任何类型。

A variable in Python is only a label, or reference to the object stored in the memory, and not a named memory location. Hence, the prior declaration of type is not needed. Because it’s just a label, it can be put on another object, which may be of any type.

Java 中,变量的类型决定了它可以存储什么,不能存储什么。在 Python 中,情况正好相反。在这里, type of data (即对象)决定了变量的类型。首先,让我们将一个字符串存储在变量中,以检查其类型。

In Java, the type of the variable decides what it can store and what not. In Python, it is the other way around. Here, the type of data (i.e. object) decides the type of the variable. To begin with, let us store a string in the variable in check its type.

>>> var="Hello"
>>> print ("id of var is ", id(var))
id of var is 2822590451184
>>> print ("type of var is ", type(var))
type of var is <class 'str'>

因此, varstring 类型。但是,它不是永久绑定的。它只是一个标签;它可以被分配给任何其他类型的对象,比如一个 float,该 float 将存储为另一个 id():

So, var is of string type. However, it is not permanently bound. It’s just a label; and can be assigned to any other type of object, say a float, which will be stored with a different id() −

>>> var=25.50
>>> print ("id of var is ", id(var))
id of var is 2822589562256
>>> print ("type of var is ", type(var))
type of var is <class 'float'>

或者一个元组。var 标签现在位于一个不同的对象上。

or a tuple. The var label now sits on a different object.

>>> var=(10,20,30)
>>> print ("id of var is ", id(var))
id of var is 2822592028992
>>> print ("type of var is ", type(var))
type of var is <class 'tuple'>

我们可以看到,每次 var 引用一个新对象时,它的类型都会发生变化。这就是为什么 Python 是 dynamically typed language

We can see that the type of var changes every time it refers to a new object. That’s why Python is a dynamically typed language.

C / C++ 和 Java 相比, Dynamic typing feature of Python 使其变得灵活。但是,它容易出现运行时错误,因此程序员必须小心。

Dynamic typing feature of Python makes it flexible compared to C/C++ and Java. However, it is prone to runtime errors, so the programmer has to be careful.