classmethod()

Converts a given function into a class method.

Syntax

classmethod(function)

Example 1

Use classmethod() to create the Codecademy class method:

class Student:
name = "Codecademy"
def print_name(obj):
print("Welcome to...", obj.name)
Student.print_name = classmethod(Student.print_name)
Student.print_name()

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Python on Codecademy

Contributors