14

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?

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
confused
  • 143
  • 1
  • 1
  • 4
  • 1
    Python is a dynamic language so it's weakly typed in some sense and strongly typed in other (there's no single definition) but the whole question is lot more complicated than that. See http://wiki.python.org/moin/StrongVsWeakTyping – Tamas Czinege Jan 08 '10 at 03:46
  • 1
    possible duplicate of [Can someone tell me what Strong typing and weak typing means and which one is better?](http://stackoverflow.com/questions/323323/can-someone-tell-me-what-strong-typing-and-weak-typing-means-and-which-one-is-be), and also [is-my-understanding-of-type-systems-correct](http://stackoverflow.com/questions/2135066/is-my-understanding-of-type-systems-correct) – nawfal Jul 24 '14 at 09:55
  • 1
    I think this is not a dupe as this is very specific to Python. It's not as if we cannot ask language specific questions on the type system anymore because strong and weak typing were explained in answers to other questions. For one thing, even if it was explained there, it would be hard to find between all the other information. – Maarten Bodewes Jun 25 '16 at 09:39

5 Answers5

46

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.

John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • 3
    I think you mean 2 for that last one, not '11'. – Ignacio Vazquez-Abrams Jan 08 '10 at 03:47
  • 1
    Oh, I see. I had it mixed up. If I want a statically-typed language for the Web, I should probably go with C# or Java then. – confused Jan 08 '10 at 03:52
  • Well, I *have* written a CGI app in C++ before, but I don't recommend it. – Ignacio Vazquez-Abrams Jan 08 '10 at 03:57
  • 1
    confused: if you like Pythons syntax, check Boo language for the CLR platform. For the JVM Scala gives you a lot more type checking and less verbose code. – Ants Aasma Jan 08 '10 at 11:33
  • 2
    @confused: No. You don't want a statically-typed language. Java and C# are large pains in the neck to work with. You need to ask your real question (as a separate question). Why do you think static typing matters? What are you doing that makes static typing "seem" so important? – S.Lott Jan 08 '10 at 11:39
6

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.

Foppe
  • 301
  • 1
  • 6
5

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.

Anurag
  • 140,337
  • 36
  • 221
  • 257
  • wrong!the biggest php problem that she weak types lang, because that you can have hidden bugs in code like undefined variables, functions etc..- maintain web app of php very hard only because that reason – Ben Apr 22 '11 at 09:14
  • 1
    @Yosef Your comment seems to be completely unrelated to this answer (which you incorrectly claim is wrong). – Asad Saeeduddin Mar 21 '14 at 14:39
3

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

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