Tuples are usually used for a small number of entries and when the position and sequence of the entries in a collection is important. To preserve the sequence of entries, tuples are designed as immutable, and this is where tuples differentiate themselves from lists.Operations on a tuple are typically faster than a regular list datatype. In cases when the values in a collection are required to be constant...
Tuesday, March 29, 2022
Monday, March 28, 2022
Lists
The list is one of the basic collection types in Python, which is used to store multiple objects using a single variable. Lists are dynamic and mutable, which means the objects in a list can be changed and the list can grow or shrink.List objects in Python are not implemented using any linked list concept but using a variable-length array. The array contains references to objects it is storing. The...
Sunday, March 27, 2022
Python data containers
Python supports several data types, both numeric as well as collections. Defining numeric data types such as integers and floating-point numbers is based on assigning a value to a variable. The value we assign to a variable determines the type of the numeric data type. Note that a specific constructor (for example, int() and float()) can also be used to create a variable of a specific data type. Container...
Saturday, March 26, 2022
When not to use OOP in Python
Python has the flexibility to develop programs using either OOP languages such as Java or using declarative programming such as C. OOP is always appealing to developers because it provides powerful tools such as encapsulation, abstraction, inheritance, and polymorphism, but these tools may not fit every scenario and use case. These tools are more beneficial when used to build a large and complex application,...
Friday, March 25, 2022
Duck typing
Duck typing, sometimes referred to as dynamic typing, is mostly adopted in programming languages that support dynamic typing, such as Python and JavaScript. The name duck typing is borrowed based on the following quote:"If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck." This means that if a bird is behaving like a duck, it will likely be a duck. The point...
Thursday, March 24, 2022
Composition
Composition is another popular concept in OOP that is again somewhat relevant to encapsulation. In simple words, composition means to include one or more objects inside an object to form a real-world object. A class that includes other class objects is called a composite class, and the classes whose...
Wednesday, March 23, 2022
Abstraction
Abstraction is another powerful feature of OOP and is mainly related to hide the details of the implementation and show only the essential or high-level features of an object. A real-world example is a car that we derive with the main features available to us as a driver, without knowing the real details of how the feature works and which other objects are involved to provide these features.Abstraction...
Tuesday, March 22, 2022
Polymorphism
In OOP, polymorphism is the ability of an instance to behave in multiple ways and a way to usethe same method with the same name and the same arguments, to behave differently in accordance with the class it belongs to.Polymorphism can be implemented in two ways: method overloading and method overriding. Let us discuss each of them.Method overloadingMethod overloading is a way to achieve polymorphism...
Monday, March 21, 2022
Extending classes with inheritance
The concept of inheritance in OOP is similar to the concept of inheritance in the real world, where children inherit some of the characteristics from their parents on top of their own characteristics.Similarly, a class can inherit elements from another class. These elements include attributes and methods....
Sunday, March 20, 2022
Using property decorators
Using a decorator to define getters and setters is a modern approach that helps to achieve the Python way of programming.If you are into using decorators, then we have a @property decorator in Python to make the code simpler and cleaner. The Car class with traditional getters and setters is updated with decorators, and here is a code snippet showing this:carexample7.pyclass Car:__mileage_units = "Mi"def...
Saturday, March 19, 2022
Protecting the data
We have seen in our previous code examples that we can access the instance attributes without any restrictions. We also implemented instance methods and we have no restriction on the use of these. We emulate to define them as private or protected, which works to hide the data and actions from the outside world.But in real-world problems, we need to provide access to the variables in a way that is...
Friday, March 18, 2022
Hiding information
We have seen in our previous code examples that we have access to all class-level as well as instance-level attributes without any restrictions. Such an approach led us to a flat design, and the class will simply become a wrapper around the variables and methods. A better object-oriented design approach is to hide some of the instance attributes and make only the necessary attributes visible to the...