Python
Module 7: Python Object-Oriented ProgrammingBeginner22 min read

Classes, Objects & __init__

Build object-oriented architectures with constructors and instance methods.

Key Takeaways & Core Concepts
  • Build object-oriented architectures with constructors and instance methods.
  • Practice writing and running standard examples.
  • Apply best practices and debugging techniques.

Classes, Objects & __init__

### Python Object-Oriented Programming (OOP)

Python is an object-oriented programming language. Almost everything in Python is an object, with its properties and methods.

#### Defining a Class and Constructor A Class is like an object constructor, or a "blueprint" for creating objects.

PYTHON Code Example
1class Course:
2 platform = "CodeZov" # Class attribute (shared by all instances)
3
4 def __init__(self, title, instructor, price):
5 # Instance attributes (unique to each instance)
6 self.title = title
7 self.instructor = instructor
8 self.price = price
9
10 def get_details(self):
11 return f"{self.title} by {self.instructor} - ₹{self.price}"
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
11 lines

#### The self Parameter The self parameter is a reference to the current instance of the class and is used to access variables and methods that belong to the class.

Instantiating and Calling Methods
1class BankAccount:
2 def __init__(self, owner, balance=0):
3 self.owner = owner
4 self.balance = balance
5
6 def deposit(self, amount):
7 self.balance += amount
8 return f"Deposited ₹{amount}. New balance: ₹{self.balance}"
9
10account = BankAccount("Vikram", 1000)
11print(account.deposit(500))
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
11 lines
Note: The deposit method updates the instance's balance attribute state.
Practice & AssessmentPython Programming Tutorial

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 0 correct(0 attempted)