Introduction to Classes
Lesson 1 of 2
  1. 1
    Python 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…
  2. 2
    A 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…
  3. 3
    We’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 …
  4. 4
    Excellent! 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…
  5. 5
    Perfect! 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 =…
  6. 6
    Now 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…
  7. 7
    Another 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…
  8. 8
    When 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!
  9. 9
    A 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…
  10. 10
    Classes 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…
  11. 11
    Inheritance 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…
  12. 12
    In 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…
  13. 13
    Sometimes you’ll want one class that inherits from another to not only take on the methods and attributes of its parent, but to override one or more of them. class Employee(object): def __init…
  14. 14
    On 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…
  15. 15
    First things first: let’s create a class to work with.
  16. 16
    Great! Now let’s add a member variable and a method to our class.
  17. 17
    Let’s go ahead and create an instance of our Triangle class.
  18. 18
    Finally, 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

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo