Comp sci: Python: class methods

Self-tutoring about Python coding: the tutor mentions an observation about Python class methods.

A Python class method can be called by referencing its containing class name rather than an instance. Such a method might be called via classname.the_method(). However, it’s the definition of the class method that’s the point herein.

One might see a class method declaration with the decorator @classmethod just above it, then with (cls) as its first argument, where (self) would be for an object method. My experience is that the decorator @classmethod is necessary, at least for IDLE to run the method as desired. However, also in my experience, (cls) can be swapped out with (something_else) and the class method will still run the same.

Source:

YouTube: Tech With Tim: Python Object Oriented Programming (OOP) – For Beginners

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply