capypad
0 day streak
python / beginner
Snippet

Basic Class Definition

Classes are templates for objects. The __init__ method is a constructor that initializes object properties.

snippet.py
python
1
2
3
4
5
6
class Book:
def __init__(self, title):
self.title = title
 
my_book = Book("Python Basics")
print(my_book.title)
Breakdown
1
class Book:
Declares a new class named Book.
2
def __init__(self, title):
The constructor method that runs when a new object is created.
3
self.title = title
Assigns the passed title to the object's instance variable.