Chevron Left Icon
Introduction to Classes
Lesson 1 of 2
Chevron Right Icon
  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…
  1. 1
    Classes can be very useful for storing complicated objects with their own methods and variables. Defining a class is much like defining a function, but we use the class keyword instead. We also use…
  2. 2
    We can use classes to create new objects, which we say are instances of those classes. Creating a new instance of a class is as easy as saying: newObject = ClassName()
  3. 3
    Classes can have member variables that store information about each class object. We call them member variables since they are information that belongs to the class object. Creating member varia…
  4. 4
    Each class object we create has its own set of member variables. Since we’ve created an object my_car that is an instance of the Car class, my_car should already have a member variable named condit…
  5. 5
    There is a special function named init() that gets called whenever we create a new instance of a class. It exists by default, even though we don’t see it. However, we can define our own _init
  6. 6
    Calling class member variables works the same whether those values are created within the class (like our car’s condition) or values are passed into the new object at initialization. We use dot not…
  7. 7
    Besides member variables, classes can also have their own methods. For example: class Square(object): def init(self, side): self.side = side def perimeter(self): return self.side …
  8. 8
    We can modify variables that belong to a class the same way that we initialize those member variables. This can be useful when we want to change the value a variable takes on based on something tha…
  9. 9
    One of the benefits of classes is that we can create more complicated classes that inherit variables or methods from their parent classes. This saves us time and helps us build more complicated…
  10. 10
    Since our ElectricCar is a more specialized type of Car, we can give the ElectricCar its own drive_car() method that has different functionality than the original Car class’s.
  11. 11
    Chances are, you won’t be designing Car classes in the real world anytime soon. Usually, classes are most useful for holding and accessing abstract collections of data. One useful class method to …

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