我遇到过一些链接,上面说Python是一种强类型语言。
然而,我认为在强类型语言中你不能这样做:
bob = 1
bob = "bob"
我认为强类型语言在运行时不接受类型更改。也许我对强/弱类型的定义是错误的(或过于简单)。
那么,Python是强类型语言还是弱类型语言呢?
我遇到过一些链接,上面说Python是一种强类型语言。
然而,我认为在强类型语言中你不能这样做:
bob = 1
bob = "bob"
我认为强类型语言在运行时不接受类型更改。也许我对强/弱类型的定义是错误的(或过于简单)。
那么,Python是强类型语言还是弱类型语言呢?
当前回答
术语“强类型”没有明确的定义。
因此,这个词的使用取决于你和谁说话。
我不认为任何没有显式声明变量类型或没有静态类型的语言是强类型的。
强类型不只是排除转换(例如,“自动”将整数转换为字符串)。它排除了赋值(即改变变量的类型)。
如果以下代码编译(解释),则该语言不是强类型的:
Foo = 1 Foo = "1"
在强类型语言中,程序员可以“依赖”类型。
例如,如果程序员看到声明,
UINT64 kZarkCount;
并且他或她知道20行之后,kZarkCount仍然是UINT64(只要它出现在同一个块中)——而不必检查中间的代码。
其他回答
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。
现有的答案大多同意Python是一种强类型语言,因为它不会隐式地将值从一种类型转换为另一种类型。这些答案提到了将字符串添加到整数的情况来支持这种说法;"foo" + 3在Python中引发TypeError,而在Javascript(通常被认为是弱类型语言)中,数字3隐式转换为字符串,然后连接,因此结果是字符串"foo3"。
但在其他一些情况下,Python确实会执行隐式类型转换:
# implicit conversion from int to float
1 + 1.0
# implicit conversion from list to bool
if []: pass
相比之下,f#(通常被认为是强类型语言)不允许这两种:
1 + 1.0;;
----^^^
error FS0001: The type 'float' does not match the type 'int'
if [] then 1 else 2;;
---^^
error FS0001: This expression was expected to have type bool but here has type 'a list
因此,“强类型”和“弱类型”语言并没有严格的二分法,相反,我们可以说Python比Javascript更强类型,但没有f#强类型。
TLDR;
Python类型是动态的,所以你可以将字符串变量更改为int(在静态语言中你不能)
x = 'somestring'
x = 50
Python类型是强的,所以你不能合并类型:
'foo' + 3 --> TypeError: cannot concatenate 'str' and 'int' objects
在弱类型Javascript中,这种情况会发生……
'foo'+3 = 'foo3'
关于类型推断
有些语言(如Java)强制显式声明对象类型
int x = 50;
像Kotlin这样的其他人只是从值本身推断它是一个int型
x = 50
但是因为这两种语言都使用静态类型,x不能从int型改变。两种语言都不允许像这样的动态变化
x = 50
x = 'now a string'
你混淆了“强类型”和“动态类型”。
我不能通过添加字符串'12'来改变1的类型,但我可以选择在变量中存储什么类型,并在程序运行时更改它。
与动态类型相对的是静态类型;变量类型的声明在程序的生命周期内不会改变。强类型的反面是弱类型;值的类型可以在程序的生命周期内更改。
我刚刚发现了一个极好的、简洁的记忆方法:
动态/静态类型表达式;强/弱类型值。