Question

l learned that Python is strong-dynamic typed language.
dynamic: type of a variable is determined at execution time NOT compiling time. For this part, I can understand that type is determined when a value(type of course) is assigned to the variable.

strong: you can NOT change the type of a variable. But this is not the real case:

>>> a = 1
>>> type(a)
<type 'int'>
>>> a = 's'
>>> type(a)
<type 'str'> 

From the code above, I can change the type of variable a from int to str.
How can this happen? Could I say Python is a weak-typed language?


EDIT:

If you can give me a code snippet that shows how strong-dynamic typing affect Python programming, I would appreciate it pretty much! During my usual coding, I never care about the strong-dynamic typing issues. It seldom affects my code function as well. Weird!


EDIT:
Conclusion from the answers:

  1. Only object/value has type attribute. Variable has no type.
  2. (Strong) Type determines what operations can be performed over/between objects/values (maybe variables referring to them).
  3. (Dynamic) Type means variable just a label (reference to object/value). This label can refer to any object/value of any type.
Was it helpful?

Solution

The key is that an object retains its type no matter what you do to it. An int is an int is an int; a str is a str; a float is a float. In a weakly typed language, you can do something like

i = "1" 
j = i + 2

and get j == 3. In python, you get

TypeError: cannot concatenate 'str' and 'int' objects

A str is always a str, and can't be treated as an int even if the string contains a number.

Try this:

for a in {1, 'abc', 3.14159}:
    print a
    print type(a)

which will produce

3.14159
<type 'float'>
1
<type 'int'>
abc
<type 'str'>

A single variable can be set to refer to any type of object - that's the "dynamic" part of it. But the object is always of the same type no matter how you refer to it - that's the "strong" part of it.

OTHER TIPS

You are not changing the type of the variable so much as reassigning a which was an int to a now brand new variable by the same name, which is a str

When you reassign a new value to a variable, I wouldn't say it is the "same" variable, since it's identifier will change. Look at this example:

a = 1
print id(a)
a = 2
print id(a)
a = "asd"
print id(a)

prints in my machine:

30925512
30925488
37467840

it means a is no longer the same object. From Python Docs:

id(object) Return the “identity” of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime.

It would be more accurate to say that Python is strongly typed because you cannot change the type of an object. Python variables are somewhat different from those in many other languages because they are nothing but names pointing to objects. Names themselves contain no type information, only objects do.

In a weakly-typed language like JavaScript, you can do something like 1 == "1" and get a true result, or do 3 - "2" and get 1, because the objects implicitly coerce between integer and string; an object's type is not "part of its identity", and objects behave like different types according to the context. In Python you can't do such things; if you want to make one type act like another, you have to actually create a new object of a new type, because an object's type is intrinsic to the object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top