Is Python a weakly typed language as variables can switch types?

对着背影说爱祢 提交于 2019-11-27 13:12:48

问题


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?


回答1:


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 [*].

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:

>>> 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.

$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.




回答2:


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

$i = 1 + "1" // $i == 2

the plus adds (math) because the . (dot) is used for concatenation, so in 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.




回答3:


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.




回答4:


Yes. That is not strong/weak typing, that is static/dynamic typing. Weak typing allows things such as 5 + '5' to equal 10.



来源:https://stackoverflow.com/questions/2025353/is-python-a-weakly-typed-language-as-variables-can-switch-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!