Skip to main content

Python Class Method Decorator @classmethod

 Python Class Method Decorator @classmethod

In Python, the @classmetho decorator is used to declare a method in the class as a class method that can be called using ClassName.MethodName(). The class method can also be called using an object of the class.

The @classmethod is an alternative of the classmethod() function. It is recommended to use the @classmethod decorator instead of the function because it is just a syntactic sugar.

@classmethod Characteristics

  • Declares a class method.
  • The first parameter must be cls, which can be used to access class attributes.
  • The class method can only access the class attributes but not the instance attributes.
  • The class method can be called using ClassName.MethodName() and also using object.
  • It can return an object of the class.

The following example declares a class method.

class Student:
    name = 'unknown' # class attribute
    def __init__(self):
        self.age = 20  # instance attribute

    @classmethod
    def tostring(cls):
        print('Student Class Attributes: name=',cls.name)

Above, the Student class contains a class attribute name and an instance attribute age. The tostring() method is decorated with the @classmethod decorator that makes it a class method, which can be called using the Student.tostring(). Note that the first parameter of any class method must be cls that can be used to access the class's attributes. You can give any name to the first parameter instead of cls.

Comments

Popular posts from this blog

Best digital marketing in Perth

Best digital marketing in Perth Introduction Your introduction into the brave new world of the digital space will be custom-tailored to your business needs requirements. You will be introduced to the crew who will be handling your project, from inception to the launch into the market. Assess It will be our job to not only know your customers but how they engage with the core products and  brand relationships . From here we break down what we research, to identify the core elements needed to engage the customer. Create It’s imperative that the design of your vessel is done right from the start. Its shape, level of focus, and attention to detail are crucial for a prosperous, lucrative, and extended journey. We will always present concepts and suggestions as per the requirement, but we truly believe this process should be a collaborative one between the creative crew of the PWD and the client. The final form will dictate its progression into the  development  and manufacturi...

Top 5 pattern-in-python

  Pattern #1: 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1 code:- n = 5 for i in range(1,n+1): for j in range(1,–i): print(j,end=” “) for k in range(–i,0,-1): print(k,end=” “) print(“\n”) Pattern #2: 5 4 3 2 1 1 2 3 4 5 5 4 3 2 2 3 4 5 5 4 3 3 4 5 5 4 4 5 5 5 code:- n = 6 for i in range(0,n): for j in range(n-1,i,-1): print(j,end=” “) for k in range(i+1,n): print(k,end=” “) print(“\n”) Pattern #3: 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1 code:- n = 5 for i in range(n,0,-1): for j in range(i): print(i,end=” “) print(“\n”) Pattern #4: 1 2 3 4 5 2 2 3 4 5 3 3 3 4 5 4 4 4 4 5 5 5 5 5 5 code:- n = 5 for i in range(1,n+1): for j in range(1,n+1): if j <= i: print(i, end=’ ‘) else: print(j, end=’ ‘) print() Pattern #5: 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 code:- n = 5 for i in range(n,0,-1): for j in range(i): print(n,end=” “) print(“\n”) for i in range(1,n+1): for j in range(i): print(n,end=” “) print(“\n”)

Difference between loc() and iloc() in Pandas DataFrame

  Difference between loc() and iloc() in Pandas DataFrame Pandas library of python is very useful for the manipulation of mathematical data and is widely used in the field of machine learning. It comprises many methods for its proper functioning.  loc()  and  iloc()  are one of those methods. These are used in slicing data from the Pandas DataFrame. They help in the convenient selection of data from the DataFrame. They are used in filtering the data according to some conditions. The working of both of these methods is explained in the sample dataset of cars. loc()  :  loc()  is label-based data selecting method which means that we have to pass the name of the row or column which we want to select. This method includes the last element of the range passed in it, unlike  iloc() .  loc()   can accept the boolean data unlike  iloc()  .  iloc() :  iloc( )  is an indexed-based selecting method which means that we ...

Multiple Choice Questions (MCQ) on Job Design

  1-Work Design is concerned with the _____ of work system in any type of organisation or institution. (A) Study (B) Design (C) Study and design (D) Management   2-High levels of productivity result in (A) Good quality (B) Lower costs (C) Higher purchasing power (D) All of the above   3-The following is (are) the objective(s) for studying work system. (A) Increasing productivity (B) Developing manpower effectiveness (C) Both (A) and (B) (D) None of the above   4-Job design is the consciously planned structuring of ____ performed by an individual or a team of persons. (A) Work effort (B) Duties (C) Responsibilities (D) All of the above   5-The following consideration(s) is (are) important human factor(s) relating to job design (A) Physiological (B) Social (C) Psychological (D) All of the above   6-The smallest work activities involve various elementary movements called (A) Therbligs (B) Therbilgs (C) Therblgis (D) Theriblgs   7-Vertical straight dials a...

Python Libraries a Data Scientist must know

  Top 21 Python Libraries a Data Scientist must know Python is an   abundant source   of   libraries . A Python library is a gathering of  functions  that assist one to  perform many actions . It has  myriad inbuilt   libraries . Python contains ample libraries for  data science . This tutorial covers  Python libraries  for a data scientist. 1. Data Cleaning and Data Manipulation Pandas NumPy Spacy SciPy 2. Data Gathering Beautiful Soap Scrapy Selenium 3. Data Visualisation Matplotlib Seaborn Bokeh Plotly 4. Data Modelling Scikit-Learn PyTorch TensorFlow Theano 5. Image Processing Scikit-Image Pillow OpenCV 6. Audio Processing pyAudioAnalysis Librosa Madmom