Is Python a Strongly-Typed Language?

Question
Some programming languages adhere to a discipline known as being "strongly typed."  This attribute governs variable assignments.  Is Python a strongly-typed language?

Answer
Technically, yes it is.  Some interviewers for jobs involving Python may ask this question and expect you to say "no it is not."

Detailed Explanation
In software development there are many people who have used Python and found its variables to be "dynamically typed" and not "statically typed."  They often characterize Python as a weakly-typed language.  Technically it is a strongly-typed language.  Guido van Rossum, the person who invented Python, explains that a weakly-typed language such as JavaScript handles the "+" operator differently from a strongly-typed language such as Python depending on a variable's value.

Here is what Van Rossum says:

"... in some languages (like JavaScript) you can add strings to numbers 'x' + 3 becomes 'x3'."

...

"In a strongly typed language (like Python) you can't perform operations inappropriate to the type of the object - attempting to add numbers to strings will fail."

Both quotes were taken from this link.  This link explains that Van Rossum was the author.

To prove Van Rossum's point consider this Python program that prints "12":

x = 5
y = 7
z = x + y
print z

Now change the Python program to have the x variable receive the string "x" but keep the other three lines the same:

x = x
y = 7
z = x + y
print z

The above modified program will print this (assuming you name the program itself a.py):

Traceback (most recent call last):
  File "a.py", line 1, in <module>
    x = x
NameError: name 'x' is not defined

So you can see that Python could be more weakly-typed.  Some people would say there is a gray zone between weakly-typed and strongly-typed.

For further details see below
StackOverflow has a discussion about Python being strongly typed here.  Relevantly and more generally, StackOverflow also has a question about strongly-typed and weakly-typed attributes in programming languages here.  The news forum for Y Combinator has some information about Python being strongly-typed.  An external blog has some more analysis of this topic here.

Leave a comment

Your email address will not be published.