---
title: "Explain the Python Class Methods."  
description: "Explain the Python Class Methods."  
author: "Anubhav Sharma"  
published: 2025-10-26  
updated: 2025-10-26  
canonical: https://www.mindstick.com/interview/34395/explain-the-python-class-methods  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 4 minutes  

---

# Explain the Python Class Methods.

> In Python, **class methods** are special methods that are **bound to the class** (not the instance).\
> They can modify or access **class-level data** shared across all instances.

### 1. Definition

A [**class method**](https://www.mindstick.com/forum/161264/how-do-you-create-a-class-and-an-object-in-python) is defined using the decorator:

```python
@classmethod
```

and it takes `cls` as the first parameter (not `self`).

### 2. Syntax

```python
class MyClass:
    class_variable = "Shared Value"

    @classmethod
    def my_class_method(cls, arg1):
        print("Class variable:", cls.class_variable)
        print("Argument:", arg1)
```

### 3. Usage Example

```python
class Student:
    school_name = "Green Valley High"

    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def change_school(cls, new_name):
        cls.school_name = new_name

# Before changing
print(Student.school_name)  # Green Valley High

# Change using class method
Student.change_school("Blue Ridge Academy")
print(Student.school_name)  # Blue Ridge Academy
```

## Explanation:

- `change_school()` is called on the **class**, not on an instance.
- It modifies the **class variable**, and the change affects **all instances**.

### 4. Difference Between Instance, Class, and Static Methods

| Method Type | Decorator | First Parameter | Can Access Instance (`self`) | Can Access Class (`cls`) | Common Use |
| --- | --- | --- | --- | --- | --- |
| Instance Method | *(None)* | `self` | Yes | Via `self.__class__` | Works on instance data |
| Class Method | `@classmethod` | `cls` | No | Yes | Modify class-level data |
| Static Method | `@staticmethod` | *(none)* | No | No | Utility methods (no access to instance/class) |

### 5. Example Showing All Three

```python
class Example:
    count = 0

    def __init__(self):
        Example.count += 1

    def instance_method(self):
        return f"Instance method called. Instance count: {self.count}"

    @classmethod
    def class_method(cls):
        return f"Class method called. Total instances: {cls.count}"

    @staticmethod
    def static_method():
        return "Static method called. No instance or class data used."
```

## Usage:

```python
obj = Example()
print(obj.instance_method())
print(Example.class_method())
print(Example.static_method())
```

## Answers

### Answer by Anubhav Sharma

> In Python, **class methods** are special methods that are **bound to the class** (not the instance).\
> They can modify or access **class-level data** shared across all instances.

### 1. Definition

A [**class method**](https://www.mindstick.com/forum/161264/how-do-you-create-a-class-and-an-object-in-python) is defined using the decorator:

```python
@classmethod
```

and it takes `cls` as the first parameter (not `self`).

### 2. Syntax

```python
class MyClass:
    class_variable = "Shared Value"

    @classmethod
    def my_class_method(cls, arg1):
        print("Class variable:", cls.class_variable)
        print("Argument:", arg1)
```

### 3. Usage Example

```python
class Student:
    school_name = "Green Valley High"

    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def change_school(cls, new_name):
        cls.school_name = new_name

# Before changing
print(Student.school_name)  # Green Valley High

# Change using class method
Student.change_school("Blue Ridge Academy")
print(Student.school_name)  # Blue Ridge Academy
```

## Explanation:

- `change_school()` is called on the **class**, not on an instance.
- It modifies the **class variable**, and the change affects **all instances**.

### 4. Difference Between Instance, Class, and Static Methods

| Method Type | Decorator | First Parameter | Can Access Instance (`self`) | Can Access Class (`cls`) | Common Use |
| --- | --- | --- | --- | --- | --- |
| Instance Method | *(None)* | `self` | Yes | Via `self.__class__` | Works on instance data |
| Class Method | `@classmethod` | `cls` | No | Yes | Modify class-level data |
| Static Method | `@staticmethod` | *(none)* | No | No | Utility methods (no access to instance/class) |

### 5. Example Showing All Three

```python
class Example:
    count = 0

    def __init__(self):
        Example.count += 1

    def instance_method(self):
        return f"Instance method called. Instance count: {self.count}"

    @classmethod
    def class_method(cls):
        return f"Class method called. Total instances: {cls.count}"

    @staticmethod
    def static_method():
        return "Static method called. No instance or class data used."
```

## Usage:

```python
obj = Example()
print(obj.instance_method())
print(Example.class_method())
print(Example.static_method())
```


---

Original Source: https://www.mindstick.com/interview/34395/explain-the-python-class-methods

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
