← Back to Python Course | Chapter 7: OOP | Lesson 2 of 11

Python Classes & Objects

A class is a blueprint, like a cookie cutter, and an object is one cookie made from it. Each object has its own details but follows the same design.
Syntax
python
class ClassName:
    attribute = value

    def method_name(self):
        # body

object_name = ClassName()
object_name.method_name()

Creating a Class

You define a class with the class keyword followed by a PascalCase name (like class BankAccount:), and if you're not ready to fill in the body yet, the pass statement is a valid placeholder that lets the file still run without a syntax error.

Example: Creating a Class

python
class BankAccount:
    pass  # placeholder body, still valid Python

print(BankAccount)

Instantiating Objects

Calling the class name like a function -- BankAccount() -- creates a new object from that blueprint; this triggers the class's __init__ method automatically, which is where you'd typically set up that object's starting attribute values.

Example: Instantiating Objects

python
class BankAccount:
    def __init__(self):
        print("Account created")  # runs automatically when the class is called

account = BankAccount()

Class vs Instance Attributes

A class attribute is defined directly inside the class body and shared by every instance (changing it affects all objects at once), while an instance attribute is set inside a method (usually __init__) via self.x and belongs to just that one object.

Example: Class vs Instance Attributes

python
class BankAccount:
    bank_name = "Cursor Bank"  # class attribute
    def __init__(self, owner):
        self.owner = owner  # instance attribute

a = BankAccount("Alex")
print(a.bank_name, a.owner)

Class Methods

Every method defined inside a class automatically receives the calling object as its first argument, conventionally named self -- that's how a method like deposit() knows which specific account's balance to update rather than a shared or ambiguous one.

Example: Class Methods

python
class BankAccount:
    def __init__(self, balance):
        self.balance = balance
    def deposit(self, amount):
        self.balance += amount  # self identifies which account's balance to update

a = BankAccount(100)
a.deposit(50)
print(a.balance)

Modifying Object Properties

You can read or reassign an object's attribute directly from outside the class using dot notation (account.balance = 500), though relying on getter/setter methods or @property is often safer once you need validation on that assignment.

Example: Modifying Object Properties

python
class BankAccount:
    def __init__(self, balance):
        self.balance = balance

a = BankAccount(100)
a.balance = 500  # reassigns the attribute directly from outside the class
print(a.balance)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting self as the first parameter of a method, as in def greet():, which raises TypeError because Python passes the instance automatically.
  2. Defining a mutable class attribute such as items = [] and expecting each object to get its own copy, when all instances share the same list.
  3. Calling a method on the class without an object, as in Dog.bark(), instead of Dog().bark().

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.