Python: Dynamic typed

According to wikipedia, “A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. In dynamic typing values have types, but variables do not; that is, a variable can refer to a value of any type. ”

In statically typed language (like Java), you define a variable of certain type during declaration and its bound to that data type. Assigning the variable with values from different data type is caught during compile-time.

But in dynamically typed languages (like Python), every variable name is bound to an object. Based on the type of value assigned, data type of variable gets decided.

[sourcecode language=”python”]
>>> var = ‘some string’
>>> print type(var)
<type ‘str’>
>>> var = 4
>>> print type(var)
<type ‘int’>

You must have observed, the type of variable var changed based on the value assigned to it, hence dynamically typed.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.