GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CODESCRACKER
  • Python
  • Java
  • C++
  • JavaScript
  • SQL
  • Quiz
  • C
  • C#
  • HTML
  • CSS
  • PHP
  • Networking
  • Operating System

This view

  • Original page
  • New address

GeezerVison is showing codescracker.com — Python classmethod decorator: @classmethod

  • Python Basics
  • Python Tutorial
  • Python Applications
  • Python Versions
  • Python environment setup
  • Python Basic Syntax
  • Python end (end=)
  • Python sep (sep=)
  • Python Comments
  • Python Identifiers
  • Python Variables
  • Python Operators
  • Python Ternary Operator
  • Python Operator Precedence
  • Python Control and Decision
  • Python Decision Making
  • Python if elif else
  • Python Loops
  • Python for Loop
  • Python while Loop
  • Python break Statement
  • Python continue Statement
  • Python pass Statement
  • Python break vs. continue
  • Python pass vs. continue
  • Python Built-in Types
  • Python Data Types
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python frozenset
  • Python Dictionary
  • List vs. Tuple vs. Dict vs. Set
  • Python Numbers
  • Python Strings
  • Python bytes
  • Python bytearray
  • Python memoryview
  • Python Misc Topics
  • Python Functions
  • Python Variable Scope
  • Python Enumeration
  • Python import Statement
  • Python Modules
  • Python operator Module
  • Python os Module
  • Python Date and Time
  • Python Exception Handling
  • Python File Handling
  • Python Advanced
  • Python Classes and Objects
  • Python @classmethod Decorator
  • Python @staticmethod Decorator
  • Python Class vs. Static Method
  • Python @property Decorator
  • Python Keywords
  • Python Keywords
  • Python and
  • Python or
  • Python not
  • Python True
  • Python False
  • Python None
  • Python in
  • Python is
  • Python as
  • Python with
  • Python yield
  • Python return
  • Python del
  • Python from
  • Python lambda
  • Python assert
  • Python Built-in Functions
  • Python All Built-in Functions
  • Python print() Function
  • Python input() Function
  • Python int() Function
  • Python len() Function
  • Python range() Function
  • Python str() Function
  • Python ord() Function
  • Python chr() Function
  • Python read()
  • Python write()
  • Python open()
  • Python Examples
  • Python Examples

Python classmethod decorator: @classmethod

The class method decorator, or @classmethod, is used to define methods inside a class as class methods. For example:

class CodesCracker:
    @classmethod
    def classfun(cls, x):
        print("The first parameter (cls):", cls)
        print("The second parameter (x):", x)

CodesCracker.classfun("Hey!")

Here is the output produced by the above Python program, demonstrating the @classmethod decorator:

The first parameter (cls): <class '__main__.CodesCracker'>
The second parameter (x): Hey!

The class methods have a reference to the class object as their first argument.

Class methods have access to what the class is. The class method is also a function, but with access to the object and its internals like fields and other methods, whereas static methods don't. The differentiation between these two is provided in class methods versus static methods in Python.

The class methods can be called either directly or using:

ClassName.ClassMethodName()

Or by using an object of the class, in this way:

ob = ClassName()
ob.ClassMethodName()

where ob is a variable that refers to the object of the class ClassName.

In the above example program, using the following statement:

CodesCracker.classfun("Hey!")

the class "CodesCracker" gets implicitly passed as the first parameter of the class method named classfun(), whereas "Hey!" will get passed as the second parameter of the class method named classfun(). That is, the "cls" parameter (the first parameter) while defining class methods using the @classmethod decorator refers to the class itself.

Python @classmethod decorator syntax

The syntax of the @classmethod decorator in Python is:

@classmethod
def myfun(cls, arg1, arg2, arg3, ..., argN):
    # definition code goes here

Just like "self," the first parameter of the normal method inside a class refers to the object instance. In the case of class methods too, the first parameter of the class method, that is, "cls" inside a class, refers to the class itself. And the rest of all the parameters, like arg1, arg2, and so on, refer to the arguments that normal functions have.

Python @classmethod decorator example

Here is an example of the @classmethod decorator in Python:

class CodesCracker:
    @classmethod
    def myfun(cls, x, y, z, str):
        print("The value of x:", x)
        print("The value of y:", y)
        print("The value of z:", z)
        print("The value of str:", str)

CodesCracker.myfun(100, 200, 300, "codescracker")

The snapshot given below shows the sample output produced by the above Python program, demonstrating the @classmethod decorator:

python classmethod decorator

Python Online Test


« Previous Tutorial Next Tutorial »
Practice Quizzes

Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes.

All Quizzes SQL Quiz Python Quiz Java Quiz C++ Quiz
Popular Topics

Explore the most popular programming and computer science tutorials on CodesCracker.

Python Tutorial Java Tutorial C++ Tutorial SQL Tutorial JavaScript Tutorial HTML Tutorial CSS Tutorial Computer Networking
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.