开发者

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

开发者 https://www.devze.com 2022-12-16 05:55 出处:网络
The way I understand it, the following is allowed in PHP because it\'s a weakly-typed language. $var = \'Hello\';

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 abo开发者_运维技巧ve 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?


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.


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.


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.


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


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

int i = 1;
i="love";

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.

i="i" + 1

not accepted for strongly typed language like python.But accepted for weakly typed like JavaScript by assuming what you intend to do

0

精彩评论

暂无评论...
验证码 换一张
取 消