views:

498

answers:

4

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?

+13  A: 

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
I think you mean 2 for that last one, not '11'.
Ignacio Vazquez-Abrams
Whoops, thanks Ignacio.
John Feminella
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
Well, I *have* written a CGI app in C++ before, but I don't recommend it.
Ignacio Vazquez-Abrams
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
@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
A: 

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
Interesting -- so Java would be weakly-type, at least in the relationship of primitive numbers and Strings.
Kaleb Brasee
+1  A: 

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
+1  A: 

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.

Afwas