Welcome to the “Classes & Objects” tutorial!
Overview
Classes are blueprints for creating objects (instances). Objects bundle data (attributes) and behavior (methods).
Defining a Class
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
return f"{self.name} says woof!"
Creating Instances
fido = Dog("Fido", 3)
print(fido.name) # Fido
print(fido.bark()) # Fido says woof!
Key Takeaways
- Use
class
keyword to define a class. self
refers to the instance.- Instantiate with
ClassName(...)
.
Happy coding!
Comments
Post a Comment