我遇到过一些链接,上面说Python是一种强类型语言。

然而,我认为在强类型语言中你不能这样做:

bob = 1
bob = "bob"

我认为强类型语言在运行时不接受类型更改。也许我对强/弱类型的定义是错误的(或过于简单)。

那么,Python是强类型语言还是弱类型语言呢?


当前回答

根据这篇wiki Python文章,Python是动态类型和强类型的(也提供了一个很好的解释)。

也许您正在考虑静态类型语言,其中类型在程序执行期间不能更改,并且在编译时进行类型检查以检测可能的错误。

你可能会对这个SO问题感兴趣:动态类型语言还是静态类型语言,这篇关于类型系统的维基百科文章提供了更多信息

其他回答

这个问题已经回答过几次了,但Python是一种强类型语言:

>>> x = 3
>>> y = '4'
>>> print(x+y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

JavaScript代码:

var x = 3    
var y = '4'
alert(x + y) //Produces "34"

这就是弱类型和强类型的区别。弱类型自动尝试从一种类型转换为另一种类型,这取决于上下文(例如Perl)。强类型从不隐式转换。

您的困惑在于误解了Python如何将值绑定到名称(通常称为变量)。

在Python中,名称没有类型,所以你可以这样做:

bob = 1
bob = "bob"
bob = "An Ex-Parrot!"

名字可以绑定到任何东西上:

>>> def spam():
...     print("Spam, spam, spam, spam")
...
>>> spam_on_eggs = spam
>>> spam_on_eggs()
Spam, spam, spam, spam

欲进一步阅读:

https://en.wikipedia.org/wiki/Dynamic_dispatch

还有稍微相关但更高级的:

http://effbot.org/zone/call-by-object.htm

Python是强类型的,因为它没有未检查的动态类型错误。换句话说,您不能违反它的类型系统。

定义

Type: set of values. It partially defines the intended usage (behaviour) of its values. It can be specified in extension with an enumeration or in intension with a predicate. Type system: system checking that values are used as intended, avoiding undefined behaviour partially. Undefined behaviour should be avoided because it can lead to late program crash or silent loss of data and production of incorrect results. Typed language: language with a type system. Type error: program error checkable by a type system. Expression: program text denoting a value. Static/dynamic type: compile-time/run-time type of an expression. The run-time type of an expression is the type of the value that it denotes. Static/dynamic type system: type system checking static/dynamic types. Statically/dynamically typed language: language with a static/dynamic type system. Static/dynamic type error: program error checkable by a static/dynamic type system. Weakly/strongly typed language: language with/without unchecked dynamic type errors. Statically typed, dynamically typed, or both imply strongly typed. Monomorphic/polymorphic expression: expression having a single dynamic type/multiple dynamic types. A monomorphic expression has single intended usage, and a polymorphic expression has multiple intended usage. Universal/ad-hoc polymorphic expression: real/virtual polymorphic expression. A real polymorphic expression denotes a single value that has multiple types, and a virtual polymorphic expression denotes multiple values that have single types. Parametric/inclusion polymorphic expression: universal polymorphic expression based on generic types/subtypes of a type (e.g. the C++ expression & denotes a single T* (T&) operator value where T is a generic type/the C++ expression std::exception denotes a single S class value where S is a generic subtype of std::exception). Overloading/coercion polymorphic expression: ad-hoc polymorphic expression based on expression/value conversion (e.g. the C++ expression + denotes int (int&, int&) and float (float&, float&) operator values/the C++ expression 3.5 denotes float and bool values).

参考

Cardelli (Luca), Wegner (Peter),“关于理解类型,数据抽象和多态性”,计算调查,第17卷,第4期,1985,第471-523页,DOI: https://doi.org/10.1145/6041.6042。

根据这篇wiki Python文章,Python是动态类型和强类型的(也提供了一个很好的解释)。

也许您正在考虑静态类型语言,其中类型在程序执行期间不能更改,并且在编译时进行类型检查以检测可能的错误。

你可能会对这个SO问题感兴趣:动态类型语言还是静态类型语言,这篇关于类型系统的维基百科文章提供了更多信息

我刚刚发现了一个极好的、简洁的记忆方法:

动态/静态类型表达式;强/弱类型值。

你混淆了“强类型”和“动态类型”。

我不能通过添加字符串'12'来改变1的类型,但我可以选择在变量中存储什么类型,并在程序运行时更改它。

与动态类型相对的是静态类型;变量类型的声明在程序的生命周期内不会改变。强类型的反面是弱类型;值的类型可以在程序的生命周期内更改。