Python Classes

Python Class Methods

Class methods

Python Class Methods

Class methods are methods that are bound to the class rather than its object. They can be called on the class itself or on an instance.

Instance Methods

Regular methods that work with instance data:

class Person:
    def __init__(self, name):
        self.name = name
    
    def greet(self):
        return f"Hello, I'm {self.name}"

person = Person("Alice")
print(person.greet())

Class Methods

Methods decorated with @classmethod:

class Person:
    count = 0
    
    def __init__(self, name):
        self.name = name
        Person.count += 1
    
    @classmethod
    def get_count(cls):
        return cls.count

print(Person.get_count())