Lesson 1

Exploring the Cosmos of Python Classes: An Introduction to Classes in Python

Introduction to Classes

Today, we delve into a key concept in Object-Oriented Programming (OOP) - classes. Our aim is to understand what classes are, how to create our own Python classes, and how to use them.

Object-Oriented Programming (OOP)

Imagine the universe of OOP, where everything is an object. What about classes? They're the blueprints for creating these objects. Just like a blueprint for a house specifies everything about the house, such as the number of rooms, the size of each room, and whether it has a backyard or not, a class in Python defines what an object will be like, too!

Class Creation in Python

Creating a class involves:

  1. Using the class keyword.
  2. Writing the class name, which should start with a capital letter.
  3. Correctly indenting the body of the class.

Let's create our first class, Star:

Python
1class Star: 2 def twinkle(self): 3 print("Twinkle, twinkle, little star...")

The Star class has a twinkle method that prints a phrase.

Did you note the twinkle method has a self parameter? In Python, self represents the instance of the class itself. It's used as the first parameter in function definitions inside the class, and if you want to call a function within the class (or refer to any data in the class), you need to use self. It's kind of like saying "me" or "myself" when you're referring to yourself in a conversation, but in this case, the conversation is all the actions happening inside the class. We will cover self in more examples in the next lessons.

In Python, proper indentation is crucial, especially in the body of a class. Python relies on indentation to determine where methods begin and end. So, ensure that your code is neat with proper indentation.

Code Walkthrough

In our Star class:

  • class denotes the class definition.
  • Star is our class name.
  • def twinkle(self): creates a method named twinkle.
  • print("Twinkle, twinkle, little star...") prints a phrase when the twinkle method is invoked.
Creating Class Instances

Once we have defined our Star class, we can use it to create individual instances, or in other words, individual stars!

An instance is a specific object created from a particular class. You can create an instance from the Star class like this:

Python
1my_star = Star()

Here, my_star is an instance of the Star class. Notice the parentheses after Star - we use them to call the class as if it were a function.

Calling a Method of the Class Instance

To call a specific method in a class, we use the name of the class instance followed by .method_name().

The below code calls the twinkle method on the my_star instance of the Star class.

Python
1my_star = Star() 2my_star.twinkle() # Prints "Twinkle, twinkle, little star..."

Note that you need to add parenthesis () at the end of the method name to call a method of the class (function), but you do not need () to get an attribute. We will cover methods and attributes in detail later in this course.

Summary

Congratulations on gaining an in-depth understanding of Python classes! We've explored class definitions, methods, and instances. Next, we'll move on to practical exercises to cement these concepts. I am excited to continue our Python journey together! Keep soaring!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.