Practical Guide to Python

Object Oriented Python Practice

Practical Guide to Python

Check out a free preview of the full Practical Guide to Python course

The "Object Oriented Python Practice" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina walks the students through the Object Oriented Python practice.


Transcript from the "Object Oriented Python Practice" Lesson

>> Earlier we talked about object oriented Python. We talked about classes and self and what the difference is. So let's open a new file and save this one as class underscore example. And we're going to be running this one from the command line rather than from the repple.

We're going to pass in several variables and save them to the instance by using the self. In this example, we added some class variables. So these are variables that exists for all instances of a class. Here we set the number of wheels to be four at the class scope.

And we can query both the instance and the class variables. When we set the instance variable to 3, we'll note that the class variable is still 4. Now inheritance is a topic that you should go through on your own time. I highly recommend that you revisit this chapter and this practice, because it's one of the foundational topics in Python.

We talked about using type, isinstance and issubclass to learn more about the different classes and objects that we're working with. And we also learned about tracebacks and exceptions. So, you've probably already seen the indentation error quite a few times as you've been working in Python. You've also seen the more general syntax error.

And there are other common exceptions that you might see. For example, if you try to divide by zero you will see a zero division error. Or if you try to get a key out of a dictionary and that key doesn't exist, you will see a key error. Adding together two unrelated types will throw an exception as well.

If you want to raise your own exceptions, you will need to inherit from the exception class. If you do that by when you define your class, passing in the exception class in parentheses, and then you can go ahead and just raise it. Having a custom exceptions in your programs is a great best practice because we want to catch exceptions that are as specific as possible.

So that we have a better idea of what's going wrong when things do inevitably go wrong. You can catch exceptions with try and accept. So, try followed by an indented block of code with what you are trying that might throw an exception, followed by an accept clause that also defines what exception you would like to catch.

You could also use the as keyword if you need a handle on that exception. For example, if you wanted to grab the error message. You can also re-raise exceptions as needed. So catching an exception and then raising a more specific one. Or catching an exception and then raising a custom exception are both things that you might see in Python code.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now