我遇到过一些链接,上面说Python是一种强类型语言。
然而,我认为在强类型语言中你不能这样做:
bob = 1
bob = "bob"
我认为强类型语言在运行时不接受类型更改。也许我对强/弱类型的定义是错误的(或过于简单)。
那么,Python是强类型语言还是弱类型语言呢?
我遇到过一些链接,上面说Python是一种强类型语言。
然而,我认为在强类型语言中你不能这样做:
bob = 1
bob = "bob"
我认为强类型语言在运行时不接受类型更改。也许我对强/弱类型的定义是错误的(或过于简单)。
那么,Python是强类型语言还是弱类型语言呢?
当前回答
这个问题已经回答过几次了,但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
其他回答
术语“强类型”没有明确的定义。
因此,这个词的使用取决于你和谁说话。
我不认为任何没有显式声明变量类型或没有静态类型的语言是强类型的。
强类型不只是排除转换(例如,“自动”将整数转换为字符串)。它排除了赋值(即改变变量的类型)。
如果以下代码编译(解释),则该语言不是强类型的:
Foo = 1 Foo = "1"
在强类型语言中,程序员可以“依赖”类型。
例如,如果程序员看到声明,
UINT64 kZarkCount;
并且他或她知道20行之后,kZarkCount仍然是UINT64(只要它出现在同一个块中)——而不必检查中间的代码。
class testme(object):
''' A test object '''
def __init__(self):
self.y = 0
def f(aTestMe1, aTestMe2):
return aTestMe1.y + aTestMe2.y
c = testme #get a variable to the class
c.x = 10 #add an attribute x inital value 10
c.y = 4 #change the default attribute value of y to 4
t = testme() # declare t to be an instance object of testme
r = testme() # declare r to be an instance object of testme
t.y = 6 # set t.y to a number
r.y = 7 # set r.y to a number
print(f(r,t)) # call function designed to operate on testme objects
r.y = "I am r.y" # redefine r.y to be a string
print(f(r,t)) #POW!!!! not good....
在一个大型系统中,上述情况会在很长一段时间内造成代码不可维护的噩梦。随便你怎么称呼它,但“动态”改变变量类型的能力只是一个坏主意……
我认为,这个简单的例子可以解释强类型和动态类型之间的区别:
>>> tup = ('1', 1, .1)
>>> for item in tup:
... type(item)
...
<type 'str'>
<type 'int'>
<type 'float'>
>>>
java:
public static void main(String[] args) {
int i = 1;
i = "1"; //will be error
i = '0.1'; // will be error
}
Python变量存储对表示该值的目标对象的无类型引用。
任何赋值操作都意味着将未类型化的引用赋值给已赋值的对象——即对象通过原始引用和新引用(已计数)共享。
值类型绑定到目标对象,而不是引用值。(强)类型检查是在执行带有该值的操作(运行时)时执行的。
换句话说,变量(技术上)没有类型——如果想要精确,那么按照变量类型来考虑是没有意义的。但是引用是自动解除引用的我们实际上是根据目标对象的类型来考虑的。
这个问题已经回答过几次了,但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