Classes and the __init__ Method
Define classes, create objects, and initialize attributes.
Classes and the __init__ Method is a free Python Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, and your progress syncs across the web and the CoddyKit app. The Python Academy course includes 4 lessons in total.
Defining a Class
Use the class keyword to define a class. By convention, class names use CapWords.
class Dog:
pass
fido = Dog()
print(type(fido)) # <class '__main__.Dog'>The __init__ Method
__init__ is called automatically when an object is created. Use it to set up instance attributes.
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
fido = Dog("Fido", "Labrador")
print(fido.name) # Fidoself Explained
self refers to the current instance. It must be the first parameter of every instance method. Python passes it automatically.
class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count += 1
c = Counter()
c.increment()
print(c.count) # 1Multiple Instances
Each instance has its own copy of instance attributes set in __init__.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p1 = Point(1, 2)
p2 = Point(5, 7)
print(p1.x, p2.x) # 1 5Default Parameter Values in __init__
Give __init__ parameters default values to make some arguments optional.
class Circle:
def __init__(self, radius=1.0):
self.radius = radius
c1 = Circle()
c2 = Circle(5)
print(c1.radius, c2.radius) # 1.0 5Instance Methods
Methods defined inside a class operate on the instance via self.
class Rectangle:
def __init__(self, w, h):
self.w = w
self.h = h
def area(self):
return self.w * self.h
r = Rectangle(4, 5)
print(r.area()) # 20Adding Attributes After Creation
You can add attributes to an instance after creation, but it is cleaner to declare all of them in __init__.
class Person:
def __init__(self, name):
self.name = name
p = Person("Alice")
p.age = 30 # added after creation
print(p.name, p.age)The __init__ Return Value
__init__ must return None. Returning anything else raises a TypeError.
class Broken:
def __init__(self):
return 42 # TypeError!
# Correct
class OK:
def __init__(self):
self.value = 42
# implicit return NoneChecking Instance Type
Use isinstance() to check if an object is an instance of a class.
class Animal:
pass
class Dog(Animal):
pass
d = Dog()
print(isinstance(d, Dog)) # True
print(isinstance(d, Animal)) # True__dict__ Attribute
Every instance stores its attributes in a __dict__ dictionary.
class Book:
def __init__(self, title, pages):
self.title = title
self.pages = pages
b = Book("Python 101", 300)
print(b.__dict__) # {'title': 'Python 101', 'pages': 300}Deleting Attributes
Use del to remove an attribute from an instance at runtime.
class Config:
def __init__(self):
self.debug = True
self.version = "1.0"
cfg = Config()
del cfg.debug
print(hasattr(cfg, "debug")) # FalseQuick Check
What is the purpose of self in Python class methods?
Recap
Classes are blueprints for objects. __init__ initializes instance attributes, self refers to the current object, and each instance maintains its own attribute dictionary.
Learn Python with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 76
- Lessons
- 320
Frequently Asked Questions
Is the “Classes and the __init__ Method” lesson free?
Yes — the full text of “Classes and the __init__ Method” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO. The Python Academy course includes 4 lessons in total.
What will I learn in “Classes and the __init__ Method”?
Define classes, create objects, and initialize attributes. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Classes and the __init__ Method” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Classes and the __init__ Method
- Instance vs Class Attributes
- Instance Methods and self
- Magic Methods: __str__ and __repr__