1

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.
Zachary
  • 1,633
  • 2
  • 22
  • 34
  • 1
    strong (or weak) typing is *not* related to the "type of a *variable* [or expression]", but rather the operations [and implicit conversions] allowed on or between *values* – user2864740 Jan 15 '14 at 07:43
  • https://wiki.python.org/moin/Why%20is%20Python%20a%20dynamic%20language%20and%20also%20a%20strongly%20typed%20language – Alexander Mihailov Jan 15 '14 at 07:45

4 Answers4

3

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.

Erik Johnson
  • 1,136
  • 6
  • 17
  • A JavaScript "object retains its type" as well. Yet JavaScript is considered weakly-typed: thus strong/weak typing is *not* merely that an "x is an x". (The expression `"1" + 2` does not change the type of any value in either Python or JavaScript.) – user2864740 Jan 15 '14 at 08:14
  • The example of the failing code is correct (and I would argue this absence of presence of implicit conversions is a lynch-pin), but most of the rest is a poor argument for strong typing. In JavaScript `[1,"foo",{}].map(function (v) { return typeof v; })` yields `["number","string","object"]` which entirely negates the ancillary argument made. – user2864740 Jan 15 '14 at 08:27
  • The question was about Python. I have only trivial experience with Javascript, so I made no attempt to describe or reference it. – Erik Johnson Jan 15 '14 at 18:58
2

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

mhlester
  • 22,781
  • 10
  • 52
  • 75
2

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.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • I never used **id(object)** statement before. Your explanation is somewhat persuasive from a different point. – Zachary Jan 15 '14 at 07:55
  • @Zachary It is the same *variable* (in so far the only way to access a variable is with a *name* in a particular scope). It simply evaluates to a different *value*. The `id` relates to the *value/object* (which *results from the evaluation of the variable-expression*) and thus cannot be used to say anything about the variable otherwise. – user2864740 Jan 15 '14 at 08:04
  • DO you mean this _id()_ function is not useful in code? How can make use of it? – Zachary Jan 15 '14 at 08:17
  • @Zachary It is rarely useful in normal code. The value it returns is an *opaque identifier* for the object. It has nothing to do with typing. – user2864740 Jan 15 '14 at 08:19
0

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.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • So, variables in Python are *like* most other languages because variables [of types that are not represented as immediate values] are nothing but names pointing to objects .. :) Python just avoids some of the name baggage and implementation-talk. – user2864740 Jan 15 '14 at 07:48
  • @user2864740: I don't know how you'd evaluate "most", but I don't really agree with what you say there. In a language like C++, a variable is not just a label for an object, it also represents the storage of that object, so in C++ doing something like `a = b` can copy `b`. In Python doing `a = b` just adds `a` as a label pointing to the object `b` points to and nothing is copied. Also, in such languages, types are associated with names, not only with values. – BrenBarn Jan 15 '14 at 07:51
  • BreBarn, so that means, in Python, we cannot get the address of a variable like in C++ &a. – Zachary Jan 15 '14 at 07:53
  • Once `T*` is introduced in C/C++/Obj-C (that is, where the variable is nothing but an indirect lookup), the end-result of just being name applies quite well. Java, C#, Scala, etc, effectively treat all "reference" types T as "T*" implicitly. Many other dynamic languages such as JavaScript, Ruby and ST arguably have a similar definition of variables/bindings as Python. (It doesn't matter is a type is associated with a variable, that's only a binding restriction: `object v`.) – user2864740 Jan 15 '14 at 07:54
  • @Zachary You cannot. But you can't do that in Java or C# (without unsafe) either. – user2864740 Jan 15 '14 at 07:59