<aside> đź’ˇ Two approach based on the way in which the type of a variable is determined.
</aside>
Example: In Python, The type is determined based on the current value - Dynamic typing
x=10
→ x
is of type int
x=7.5
→ x
is of type float
<aside> đź’ˇ An uninitialized name has no type.
</aside>
Static Typing - Associate a type in advance with a name.
int a
, float x
, … → a=7.5
is no longer legal.We do we need static typing when it is more convenient to use dynamic typing ( Where we don’t need to declare variables in advance)?
In Dynamic typing, catching certain errors(like typos) will be very difficult.
Example:
def factors(n):
factorlist=[]
for i in range(1,n+10:
if n%i == 0:
factorlst = factorlist + [i] # There is a typo in this line!!
return factorlist
This makes debugging the code difficult.
Using the notion of Empty User defined objects
Node
None
l
to None
, this variable l
will not be associated to any type. i.e., After setting l
to None
, Python no longer understands that l
is a linked list.(float,float)
, and a point in 3D is the triple (float,float,float)
point2d
and point3d
which are the synonyms for (float,float)
and (float,float,float)
, respectively.<aside> đź’ˇ Identifying errors as early as possible can save cost and effort.
</aside>
Why?