Python is both a strongly typed and a dynamically typed language
Strong typing?means that variables do have a type and that the type matters when performing operations on a variable.
Dynamic typing?means that the type of the variable is determined only during run time.
other hand, types in Python are determined during run-time as opposed to compile-time and thus programmers are not required to declare variables before using them in the code.Technically, a variable?is created when it is assigned a value for the first time in the code.
This means that a variable must first be assigned a value before it is referenced in the code otherwise an error will be reported. And as mentioned earlier, the variable assignment never comes with a type definition since the type is stored with objects and not with variables/names.
Every time a variable is identified, it automatically gets replaced with the object in memory it references.
Relationship between objects, variables and references:
To sum up, every time we assign variables Python undertakes the three following steps:
1.Create an object in memory that holds the value
2.If the variable name does not already exist in the name space, go ahead and create it
3.Assign the reference to the object (in memory) to the variable
A variable, is a symbolic name in a system table that holds links (i.e. references) to objects.
In other words, references are pointers from variables to objects. In Python though, variables do not have a type.
Therefore, it is possible to assign objects of different type to the same variable name, as shown below.
a = 1
a = 'Hello World'
a = False
In the first line, variable a is being assigned with the reference to the integer object with value 1 .Likewise, the second line changes the reference of variable a to a different object of type string while the last line changes the reference so that a now points to a boolean object.
When we refer to objects we actually mean a piece of allocated memory that is capable of representing the value we wish.
This value can be an integer, a string or of any other type. Apart from the value, objects also come with a couple of header fields.
These fields include the type of the object as well as its reference counter which is used by the Garbage Collector to determine whether it is fine to reclaim the memory of unused objects.
And since Python objects are capable of knowing their own type, variables don’t have to remember this piece of information.
Example:
?One of the key features of Python is that everything is an object, and the type is just one attribute of an object.
n = 5
dir(n)
As an illustration, we can assign a single integer to a variable, and use the Python built-in function dir for finding out the attributes of the object. If you execute the following two lines in an interactive interpreter, it should become clear that a Python integer is much more complex than just a number.
a = 7
b = 6
c = a + b
there are several steps Python needs to do:
1.Check the types of both operands
2.Check whether they both support the + operation
3.Extract the function that performs the + operation (due to operator overloading objects can have a custom definition for addition)
4.Extract the actual values of the objects
5.Perform the + operation
6.Construct a new integer object for the result
?Due to the fact that Python is dynamically typed, the interpreter cannot know
beforehand what type of objects one is dealing with, and everytime two variables are added one needs to perform all the above steps.
Interview Question regarding dynamic type in python:
1.?What type of a language is python? Interpreted or Compiled?
Ans:Python is an interpreted, interactive, object oriented programming language.Python is an interpreted language, as opposed to a compiled one, though the distinction can be blurry because of the presence of the bytecode compiler. This means that source files can be run directly without explicitly creating an executable which is then run.
2. Is python is static type or dynamic type?
Ans:Dyanmic
3.Is python is strongly typed or weakly typed language?
Ans:Srongly