Posts

Showing posts with the label Class methods

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...

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 insta...