Introduction to Classes
Learn what classes are, why they're important, and how to use them effectively.
StartIntroduction to Classes
Lesson 1 of 2
- 1Python is an object-oriented programming language, which means it manipulates programming constructs called objects. You can think of an object as a single data structure that contains data as we…
- 2A basic class consists only of the class keyword, the name of the class, and the class from which the new class inherits in parentheses. (We’ll get to inheritance soon.) For now, our classes wi…
- 3We’d like our classes to do more than… well, nothing, so we’ll have to replace our pass with something else. You may have noticed in our example back in the first exercise that we started our …
- 4Excellent! Let’s make one more tweak to our class definition, then go ahead and instantiate (create) our first object. So far, init() only takes one parameter: self. This is a Python conve…
- 5Perfect! Now we’re ready to start creating objects. We can access attributes of our objects using dot notation. Here’s how it works: class Square(object): def init(self): self.sides =…
- 6Now that you’re starting to understand how classes and objects work, it’s worth delving a bit more into init() and self. They can be confusing! As mentioned, you can think of init() as the…
- 7Another important aspect of Python classes is scope. The scope of a variable is the context in which it’s visible to the program. It may surprise you to learn that not all variables are accessib…
- 8When a class has its own functions, those functions are called methods. You’ve already seen one such method: init(). But you can also define your own methods!
- 9A class can have any number of member variables. These are variables that are available to all members of a class. hippo = Animal(“Jake”, 12) cat = Animal(“Boots”, 3) print hippo.is_alive hipp…
- 10Classes like Animal and Fruit make it easy to understand the concepts of classes and instances, but you probably won’t see many zebras or lemons in real-world programs. However, classes and object…
- 11Inheritance is a tricky concept, so let’s go through it step by step. Inheritance is the process by which one class takes on the attributes and methods of another, and it’s used to express an *i…
- 12In Python, inheritance works like this: class DerivedClass(BaseClass): # code goes here where DerivedClass is the new class you’re making and BaseClass is the class from which that new class i…
- 14On the flip side, sometimes you’ll be working with a derived class (or subclass) and realize that you’ve overwritten a method or attribute defined in that class’ base class (also called a *parent…
- 15First things first: let’s create a class to work with.
- 16Great! Now let’s add a member variable and a method to our class.
- 17Let’s go ahead and create an instance of our Triangle class.
- 18Finally, let’s create an Equilateral class that inherits from our Triangle class. (An equilateral triangle is a triangle whose angles are all 60˚, which also means that its three sides are equal in…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory