Skip to main content
Classes contain two things: attributes (to store data) and methods (to add actions and behavior). Let’s look at how to define and use the different types of attributes and methods in Python using a bank Account class.

1. Attributes: Storing Data

Attributes are variables bound to a class or an instance of a class.

Instance Attributes (Unique to each object)

Instance attributes are defined inside the __init__ method using the self prefix. Each object created has its own unique copy of these values.

Class Attributes (Shared by all objects)

Class attributes are defined directly inside the class body but outside any methods. They are shared across all instances of that class.

2. Methods: Adding Behavior

Methods are functions defined inside a class that execute operations. There are three types of methods in Python:

A. Instance Methods

These are the most common type. They always take self as the first argument, representing the specific object calling the method. They can read or modify the object’s instance attributes.

B. Class Methods

Class methods are bound to the class itself, not to the instances. They are marked with the @classmethod decorator and take cls as the first parameter (representing the class object). They are used to read or modify class-level attributes.

C. Static Methods

Static methods do not take self (instance) or cls (class) as parameters. They act like regular functions, but are nested inside the class namespace because they relate to the class conceptually. They are marked with the @staticmethod decorator.

Complete Unified Example

Here is how all these elements work together inside the Account class:

Data Encapsulation: Public, Protected, and Private Attributes

Encapsulation restricts direct access to some of an object’s components, which is useful for preventing accidental modifications. In Python, encapsulation is implemented via naming conventions:

1. Public Attributes (Default)

Any attribute without leading underscores is public and can be accessed or modified from outside the class:

2. Protected Attributes (Single Underscore _)

Attributes starting with a single underscore (e.g., _balance) indicate that they are intended for internal class use and subclasses.
  • Note: Python does not enforce this. It is a convention (a warning to other developers). The variable can still be accessed and modified from outside the class.

3. Private Attributes (Double Underscore __)

Attributes starting with double underscores (e.g., __pin) trigger Name Mangling. Python renames these variables internally to _ClassName__attributeName to prevent easy access and namespace collisions in subclassing.

Private Data Security in Python:

Name mangling is not a true security feature or strong encryption. It is only a naming mechanism. You can still access and modify a private attribute outside the class by using its mangled name:
Therefore, double underscores should be used to prevent name conflicts in subclass inheritance rather than to secure sensitive private data.

Common mistakes


What’s next?

Let’s explore inheritance - how classes can build on other classes.

Inheritance

Build on existing classes