Python 是一种弱类型语言,因为变量可以切换类型吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2025353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is Python a weakly typed language as variables can switch types?
提问by confused
The way I understand it, the following is allowed in PHP because it's a weakly-typed language.
根据我的理解,PHP 允许以下内容,因为它是一种弱类型语言。
$var = 'Hello';
$var = 5;
I just installed a Windows version of Python 2.6 and I was expecting it NOT to let me change type just like that, but the Python equivalent of the above code works just like in PHP yikes!
我刚刚安装了 Python 2.6 的 Windows 版本,我希望它不会让我像那样更改类型,但是与上述代码等效的 Python 就像在 PHP 中一样工作!
>>> var = "Hello"
>>> type(var)
<type 'str'>
>>> var = 5
>>> type(var)
<type 'int'>
Is my understanding of weak/strong typing flawed?
我对弱/强类型的理解有缺陷吗?
回答by John Feminella
Your example demonstrates dynamictyping, not weak typing. Dynamic typing generally means that the type of data an object can store is mutable; any target may hold a binding to any kind of object. Contrast that with, say, C#, which is statically typed [*].
您的示例演示了动态类型,而不是弱类型。动态类型通常意味着对象可以存储的数据类型是可变的;任何目标都可以绑定到任何类型的对象。对比一下,比如说,C#,它是静态类型的 [*]。
int i = 5; // Okay.
i = "5"; // Illegal! i can only hold integers.
Strong typing means that once assigned a value of a particular kind, objects obey strict rules about how they can interact with other objects of various types. Weak typing means that such rules are more relaxed. This doesn't mean that strongly typed languages are necessarily superior in any way; it's just a language design choice.
强类型意味着一旦分配了特定类型的值,对象就如何与其他各种类型的对象进行交互遵守严格的规则。弱类型意味着这样的规则更加宽松。这并不意味着强类型语言在任何方面都必须优越;这只是一种语言设计选择。
Python is considered strongly typed because objects have a distinct notion of what they type they are. Incompatible operations between objects cause errors:
Python 被认为是强类型的,因为对象对它们的类型有明确的概念。对象之间的不兼容操作导致错误:
>>> 1 + 1 # Add two integers.
2
>>> "1" + "1" # Concatenate two strings.
'11'
>>> 1 + int("1") # Add two integers.
2
>>> "1" + str(1) # Concatenate two strings.
'11'
>>> 1 + "1" # Undefined! Adding integers and strings is meaningless.
Traceback (most recent call last):
File "", line 5, in ?
TypeError: unsupported operand type(s) for +: 'int' and 'str'
But in PHP, the rules are much more relaxed about what is acceptable. Thus it is considered more weakly typed than some other languages.
但在 PHP 中,关于可接受的规则要宽松得多。因此,它被认为比其他一些语言更弱类型。
$x = 1 + "1"; // x is 2
[*] Technically, as of C# 4, C# is statically typed but with opt-in dynamic typing on a per-binding basis, thanks to the dynamic
keyword. A lot of languages these days are adding dynamic capabilities and blurring the lines, so it's becoming increasingly harder to say that "language X is dynamic" and "language Y is static". It's much more of a sliding scale or a spectrum than it is a binary property.
[*] 从技术上讲,从 C# 4 开始,C# 是静态类型的,但由于dynamic
关键字的存在,可以在每个绑定的基础上选择加入动态类型。如今,许多语言都在添加动态功能并模糊界限,因此越来越难说“语言 X 是动态的”和“语言 Y 是静态的”。它更像是一个滑动比例或频谱,而不是一个二元属性。
回答by Afwas
One addition the the first answer: It's a tad more complicated because in python the + operator is overloaded meaning it would both add (math) and concatenate (glue two strings). In the php example
添加第一个答案:它有点复杂,因为在 python 中, + 运算符被重载,这意味着它会添加(数学)和连接(粘合两个字符串)。在 php 示例中
$i = 1 + "1" // $i == 2
the plus adds (math) because the . (dot) is used for concatenation, so in php
加号添加(数学),因为 . (dot) 用于连接,所以在 php 中
$i = 1 . "1" // $i == "11"
What I'm trying to make clear is that every weakly typed / dynamic language deals with this in it's own way.
我想说明的是,每种弱类型/动态语言都以自己的方式处理这个问题。
回答by Anurag
There's no real definition of weak typing or strong typing. Its all about implicit type conversions and has nothing to do with static/dynamic typing.
弱类型或强类型没有真正的定义。它都是关于隐式类型转换的,与静态/动态类型无关。
A statically typed language like Java canbe weakly typed (not that is it), and a dynamically typed language like PHP canbe strongly typed (not that it is).
像 Java 这样的静态类型语言可以是弱类型的(不是那样),而像 PHP 这样的动态类型语言可以是强类型(不是那样)。
A weakly typed language is more liberal in what data types can be mixed in certain operations.
弱类型语言在某些操作中可以混合哪些数据类型方面更加自由。
回答by Ignacio Vazquez-Abrams
Yes. That is not strong/weak typing, that is static/dynamic typing. Weak typing allows things such as 5 + '5'
to equal 10.
是的。这不是强/弱类型,而是静态/动态类型。弱类型允许诸如5 + '5'
等于 10 之类的东西。