capypad
0 day streak
python / beginner
Snippet

Defining a Basic Class

Classes are blueprints for objects. The __init__ method is a constructor that initializes the object's attributes.

snippet.py
python
1
2
3
4
5
6
class Pet:
def __init__(self, name):
self.name = name
 
my_pet = Pet("Luna")
print(my_pet.name)
Breakdown
1
def __init__(self, name):
The special method that runs when a new instance of the class is created.
2
self.name = name
Stores the provided name into the specific object's 'name' attribute.