Python 是弱类型语言吗?因为变量可以切换类型?
据我了解,PHP 中允许以下内容,因为它是一种弱类型语言。
$var = 'Hello';
$var = 5;
我刚刚安装了 Windows 版本的 Python 2.6,我希望它不会让我像那样更改类型,但是上述代码的 Python 等效项就像在 PHP 中一样工作,哎呀!
>>> var = "Hello"
>>> type(var)
<type 'str'>
>>> var = 5
>>> type(var)
<type 'int'>
我对弱/强类型的理解有缺陷吗?
The way I understand it, the following is allowed in PHP because it's a weakly-typed language.
$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!
>>> var = "Hello"
>>> type(var)
<type 'str'>
>>> var = 5
>>> type(var)
<type 'int'>
Is my understanding of weak/strong typing flawed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的。这不是强/弱类型,而是静态/动态类型。弱类型允许
5 + '5'
等于 10。Yes. That is not strong/weak typing, that is static/dynamic typing. Weak typing allows things such as
5 + '5'
to equal 10.用简单的语言静态/动态类型:允许或不允许更改已声明变量的数据类型。
从整数到字符串的更改是
例如,对于 Java、C、C++ 等静态类型语言, 错误的。但对于像 JavaScript 这样的动态类型,python
强/弱类型处理它允许在操作中混合不同数据类型的程度。
对于像 python 这样的强类型语言来说不被接受。但是对于像 JavaScript 这样的弱类型语言来说,通过假设你打算做什么就可以接受
In simple language static/dynamic typing: Allows or not, a change in data type of a declared variable.
example is a change from integer to string
wrong for static typed lang like Java,c, c++. But right for dynamic type like JavaScript , python
Strongly/weakly typed deals with the extent at which it allows mixing of different data types in operations.
not accepted for strongly typed language like python.But accepted for weakly typed like JavaScript by assuming what you intend to do
您的示例演示了动态类型,而不是弱类型。动态类型通常意味着对象可以存储的数据类型是可变的;任何目标都可以绑定到任何类型的对象。与静态类型的 C# 相比 [*]。
强类型意味着一旦分配了特定类型的值,对象就会遵守关于如何与其他各种类型的对象交互的严格规则。弱类型意味着此类规则更加宽松。这并不意味着强类型语言在任何方面都一定是优越的。这只是一种语言设计选择。
Python 被认为是强类型的,因为对象对于它们的类型有一个独特的概念。对象之间的不兼容操作会导致错误:
但在 PHP 中,关于可接受的规则要宽松得多。因此,它被认为比其他一些语言更弱类型。
[*] 从技术上讲,从 C# 4 开始,C# 是静态类型的,但由于
dynamic
关键字,可以在每个绑定的基础上选择动态类型。如今,许多语言都添加了动态功能并模糊了界限,因此说“语言 X 是动态的”和“语言 Y 是静态的”变得越来越困难。它更像是一个滑动尺度或一个频谱,而不是一个二元属性。Your example demonstrates dynamic typing, 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 [*].
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:
But in PHP, the rules are much more relaxed about what is acceptable. Thus it is considered more weakly typed than some other languages.
[*] 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.对第一个答案进行补充:
它有点复杂,因为在 python 中 + 运算符被重载,这意味着它会添加(数学)和连接(粘合两个字符串)。
在 php 示例中,
加号添加了(数学),因为 . (点)用于连接,所以在 php 中
我想澄清的是,每种弱类型/动态语言都以自己的方式处理这个问题。
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
the plus adds (math) because the . (dot) is used for concatenation, so in php
What I'm trying to make clear is that every weakly typed / dynamic language deals with this in it's own way.
弱类型或强类型没有真正的定义。它都是关于隐式类型转换,与静态/动态类型无关。
像 Java 这样的静态类型语言可以是弱类型的(不是这样的),而像 PHP 这样的动态类型语言可以是强类型的(不是这样的)。
弱类型语言对于在某些操作中可以混合哪些数据类型更加自由。
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 can be weakly typed (not that is it), and a dynamically typed language like PHP can be strongly typed (not that it is).
A weakly typed language is more liberal in what data types can be mixed in certain operations.