Posts

Python Tutorial 42 --- "Abstraction & Encapsulation (OOPS 7)"

 Python Tutorial 42 --- "Abstraction & Encapsulation (OOPS 7)" Our today's tutorial is based mainly on theory because coding is not just about learning new concepts but also about understanding them. Let us give our  PyCharm  some rest and try to learn some theory about Abstraction and Encapsulation, which are very important concepts for our tutorials ahead. What is Abstraction? Abstraction  refers to hiding the unnecessary details so that the focus could be on the whole product instead of parts of the project separately. It is a mechanism that represents the important features without including implementation details .  Abstraction helps us in partitioning the program into many independent concepts so, we may hide the irrelevant details in the code. It offers the greatest flexibility when using abstract data type objects in different situations. Example of Abstraction Let us take the example of a car. It has an engine, tires, windows, steering wheel, etc. All these t

Python Tutorial 41 --- "Static Methods In Python (OOPS 6)"

 Python Tutorial 41 --- "Static Methods In Python (OOPS 6)" Static Method in Python: We know that a static method in Python is treated differently than in other languages. Static methods in Python are extremely similar to python class methods, but the difference is that a static method is bound to a class rather than the objects for that class. To define a static method, we use  @staticmethod  decorator, which is a built-in decorator. Also, there is no need to import any module to use decorators. Using a static method in a class, we permit it to be accessed only by the class objects or inside the class. There are few limitations related to static methods, such as: Unlike, class method a static method cannot alter or change any variable value or state of the class. Static methods do not have any knowledge related to class Static methods do not require any knowledge related to the class that they are built-in. They are only formed in a class so that only the class instances can

Python Tutorial 40 --- "Class Methods As Alternative Constructors (OOPS 5)"

 Python Tutorial 40 --- "Class Methods As Alternative Constructors (OOPS 5)" In this tutorial, we will learn how we can convert a class method into an alternating constructor. This tutorial is not about "what," as we have seen in previous tutorials, where we learn about new concepts or functionality. But this one is about" how." We will focus more on implementation as we are already familiar with all the concepts we are going to use. In this tutorial, we are going to learn some new skills or techniques other than a new concept. We learned about constructor and its functionality in tutorial #55, where we had to set all the values as parameters to a constructor. Now we will learn how to use a method as a constructor. It has its own advantages. By using a method as a constructor, we would be able to pass the values to it using a string. Note that we are talking about a class method, not a static method. The parameters that we have to pass to our constructor w

Python Tutorial 39 --- "Class Methods in Python(OOPS 4)"

Python Tutorial 39 --- "Class Methods in Python(OOPS 4)" A s we have observed in the previous tutorials, we cannot change the value of a variable defined in the class from outside, using an object. Instead, if we try that, a new instance variable will be created for the class having the value we assigned. But no change will occur in the original value of the variable. We saw the use of self keyword in the previous tutorial. In this tutorial, we are going to know the working of a new keyword i.e., cls. Class methods take cls parameter that points to the class and not the object instance when the method is called. Syntax: class myClass : @classmethod def myfunc ( cls , arg1 , arg2 , . . . ) : . . . . Copy myfunc  defines the function that needs to be converted into a class method returns:  @classmethod returns a class method for function. Because the class method only has access to  cls  argument, it cannot modify object instance state

Python Tutorial 38 --- "Self & __init__() (Constructors)(OOPS 3)"

 Python Tutorial 38 --- "Self & __init__() (Constructors)(OOPS 3)" Doc --- In this tutorial, we will be discussing methods and constructors in detail. If you are familiar with any other object-oriented programming language, then the concept will be easy for you to grasp. So, let us begin with the Method. Method: A method is just like a function, with a  def  keyword and a single parameter in which the name of the object has to be passed. The purpose of the method is to show all the details related to the object in a single go. We choose variables that we want the method to take but do not have to pass all of them as parameters. Instead, we have to set the parameters we want to include in the method during its creation. Using methods make the process simpler and a lot faster.  Self keyword: The self keyword is used in the method to refer to the instance of the current class we are using. The self keyword is passed as a parameter explicitly every time we define a method. de

Python Tutorial 37 --- "Instance & Class Variables(OOPS 2)"

 Python Tutorial 37 --- "Instance & Class Variables(OOPS 2)" When working with objects in Python, there are two types of variables we have to work with i.e. instance variables and class variables. But what do these types of variables mean, and how do they work? OOP allows the variables to be used at the class level or the instance level. In this tutorial, we are going to learn about the two different kinds of variables associated with a class and the difference between them. The variables are: Instance variable Class variable Instance variable: "Instance variable are the variables for which the value of the variable is different for every instance." We can also say that the value is different for every object that we create. Let us dive into some in-depth explanation. When we create a class, we define a few variables along with it. For example, we have created a class of Students, and we have defined a variable age. All the students cannot have the same age in a

Python Tutorial 36 --- "Creating our first Class(OOPS1)"

 Python Tutorial 36 --- "Creating our first Class(OOPS1)" Let's begin our Learning: As procedure-oriented programming focuses on functions, object-oriented programming stresses on objects. An object is simply a collection of data and methods and a class is a blueprint for that object. We have already discussed the OOP in Python in the previous  Classes & Objects(OOPS)  tutorial. This tutorial is based on creating objects and classes.  Defining a Class in Python Like function, definitions begin with the keyword def, class begins with a  class  keyword. class MyClass : '''This is a docstring.''' pass Copy A class is a blueprint from which objects are created. Creating a new class creates a new type of object which allows the new instances of that type to be made. Each class instance has attributes attached so that it could maintain its state. Class instances can also have methods that are defined by its class for modifying its state.