Let’s take a Python code example to see if a variable can change type:
This confirms that the type of variable “a” is allowed to change and Python correctly infers the type as it changes.
Let’s take another example of Dynamic Typing in Python :
In Python, we don’t really have a good idea of what are the types that this function deals with and also what the type of the return value is going to be.
Static Typing is opposite to Dynamic Typing. In Static Typing, type checking is performed during compile time. It means that the type of a variable is known at compile time. For some languages, the programmer must specify what type each variable is (e.g C, C++, Java), other languages offer some form of type inference(e.g. Scala, Haskell).
With Static Typing, variables generally are not allowed to change types.
Let’s look at a simple example from a statically typed language. Consider the following Java snippet;
The first line declares that the variable “a” is bound to the string type at compile time. In the second line, “a” is assigned a value which is not a string object. For example, if we say, a =5, the compiler would raise an error because of incompatible types.
Duck Typing is a concept related to Dynamic Typing, where the type or the class of an object is less important than the method it defines.Using Duck Typing, we do not check types at all. Instead we check for the presence of a given method or attribute.
The reason behind the name is the duck test: “If it looks like a duck, swims like a duck, and quack like a duck, then it probably is a duck”.
Let’s take a simple Python code example to understand it clearly:
The classes which have an implementation of “quack” method will be able to call (Duck, Goose objects) but for the “Dog” class which does not provide an implementation of “quack” method, gets a “AttributeError” if we try to pass an object.
- Duck Typing in Python
- Gradual typing in Python
- Class or Static Variables in Python
- Bound, unbound, and static methods in Python
- wxPython - Remove static text on clicking button
- Python | Get a google map image of specified location using Google Static Maps API
- class method vs static method in Python
- Type Hints in Python
- Type Conversion in Python
- Python | type() function
- type and isinstance in Python
- PyQt5 QCalendarWidget - Getting children using the type
- PyQt5 QCalendarWidget - Getting child using its type
- PyQt5 QColorDialog - Child by the type
- Python | Float type and its methods
- PyQt5 QSpinBox - Getting letter spacing type
- Python - Test if all elements in list are of same type
- Python - Split heterogeneous type list
- Python - Pearson type-3 Distribution in Statistics
- How to Check the Data Type in Pandas DataFrame?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.