Learn Python: Classes

Learn about the differences between data types in Python and how to create your own classes, objects, and interfaces.

Start[missing "en.views.course_landing_page.learn-python-3.course_illustration" translation]
Chevron Left Icon
Learn Python: Classes
Lesson 1 of 2
Chevron Right Icon
  1. 1

    Python equips us with many different ways to store data. A [...] is a different kind of number from an [...] , and we store different data in a [...] than we do in a [...] . These are known a...

  2. 2

    A class is a template for a data type. It describes the kinds of information that class will hold and how a programmer will interact with that data. Define a class using the [...] keyword. PEP...

  3. 3

    A class doesn't accomplish anything simply by being defined. A class must be instantiated. In other words, we must create an instance of the class, in order to breathe life into the schematic. ...

  4. 4

    A class instance is also called an object. The pattern of defining classes and creating objects to represent the responsibilities of a program is known as Object Oriented Programming or OOP. I...

  5. 5

    When we want the same data to be available to every instance of a class we use a class variable. A class variable is a variable that's the same for every instance of the class. You can define a...

  6. 6

    Methods are functions that are defined as part of a class. The first argument in a method is always the object that is calling the method. Convention recommends that we name this first argument ...

  7. 7

    Methods can also take more arguments than just [...] : [...] Above we defined a [...] class, instantiated it, and used it to convert 5 miles into kilometers. Notice again that even though [...

  8. 8

    There are several methods that we can define in a Python class that have special behavior. These methods are sometimes called "magic", because they behave differently from regular methods. Another ...

  9. 9

    We've learned so far that a class is a schematic for a data type and an object is an instance of a class, but why is there such a strong need to differentiate the two if each object can only have t...

  10. 10

    Instance variables and class variables are both accessed similarly in Python. This is no mistake, they are both considered attributes of an object. If we attempt to access an attribute that is ne...

  11. 11

    Since we can already use dictionaries to store key-value pairs, using objects for that purpose is not really useful. Instance variables are more powerful when you can guarantee a rigidity to the da...

  12. 12

    Attributes can be added to user-defined objects after instantiation, so it's possible for an object to have some attributes that are not explicitly defined in an object's constructor. We can use th...

  13. 13

    One of the first things we learn as programmers is how to print out information that we need for debugging. Unfortunately, when we print out an object we get a default representation that seems fai...

  14. 14

    So far we've covered what a data type actually is in Python. We explored what the functionality of Python's built-in types (also referred to as primitives) are. We learned how to create our own d...

  1. 1

    Classes are designed to allow for more code reuse, but what if we need a class that looks a lot like a class we already have? If the bulk of a class's definition is useful, but we have a new use ca...

  2. 2

    There's one very important family of class definitions built in to the Python language. An Exception is a class that inherits from Python's [...] class. We can validate this ourselves using th...

  3. 3

    Inheritance is a useful way of creating objects with different class variables, but is that all it's good for? What if one of the methods needs to be implemented differently? In Python, all we have...

  4. 4

    Overriding methods is really useful in some cases but sometimes we want to add some extra logic to the existing method. In order to do that we need a way to call the method from the parent class. P...

  5. 5

    You may be wondering at this point why we would even want to have two different classes with two differently implemented methods to use the same method name. This style is especially useful when we...

  6. 6

    All this talk of interfaces demonstrates flexibility in programming. Flexibility in programming is a broad philosophy, but what's worth remembering is that we want to implement forms that are famil...

  7. 7

    One way that we can introduce polymorphism to our class definitions is by using Python's special dunder methods. We've explored a few already, the constructor [...] and the string representation ...

  8. 8

    Python offers a whole suite of magic methods a class can implement that will allow us to use the same syntax as Python's built-in data types. You can write functionality that allows custom defined ...

  9. 9

    In this lesson, we learned more complicated relationships between classes. We learned: How to create a subclass of an existing class. How to redefine existing methods of a parent class in a su...

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

Learn Python: Classes

Start[missing "en.views.course_landing_page.learn-python-3.course_illustration" translation]