0% found this document useful (0 votes)
0 views

Simple Explanation of Class Methods in Python

The document explains class methods and static methods in Python. Class methods operate on the class itself and can access class attributes, while static methods do not depend on instance or class attributes and behave like regular functions. Both types of methods serve specific purposes, such as accessing shared data or organizing utility functions within a class.

Uploaded by

sushnt0345
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Simple Explanation of Class Methods in Python

The document explains class methods and static methods in Python. Class methods operate on the class itself and can access class attributes, while static methods do not depend on instance or class attributes and behave like regular functions. Both types of methods serve specific purposes, such as accessing shared data or organizing utility functions within a class.

Uploaded by

sushnt0345
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Simple Explanation of Class Methods in Python


A class method in Python is a method that operates on the class itself rather than individual
instances.

Breaking It Down:

1. Class Attribute (total_customers)

o This belongs to the class, not to any specific object.

o It keeps track of how many Bank customers exist.

2. Constructor (__init__ method)

o When a new Bank object (customer) is created, the total number of customers
increases.

3. Class Method (@classmethod)

o This method, get_total_customers(), can access class attributes


(total_customers).

o It uses cls (class reference) instead of self (instance reference).

Assignment:
Why Use @classmethod?

✅ Access class attributes (shared data for all instances).


✅ Modify class attributes if needed.
✅ Useful for factory methods (creating objects in different ways).

2.Simple Explanation of Static Methods in Python

A static method in Python is a method that does not depend on instance or class attributes. It
is just a regular function inside a class.

Breaking It Down:

1. @staticmethod Decorator
o Marks the method as a static method.

o It does not use self (instance) or cls (class).

o It behaves like a normal function inside the class.

2. Method bank_policy()

o This method simply returns a bank policy statement.

o It does not need any instance or class-related data.

How It Works:

 Bank.bank_policy() directly calls the static method without creating an object.

 It prints:
"Minimum balance requirement is $1000."

Why Use @staticmethod?

✅ When a function does not need instance (self) or class (cls) access.
✅ Keeps the code organized inside the class, instead of being a separate function.
✅ Useful for utility functions (e.g., formatting, validation, business rules).

You might also like