Watching:

How a Class can Reuse Code and Data in Python 3


show more Reusing code and data with a class provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training show less
please wait ...

Reusing code and data with a class

Object-oriented programming is a very powerful model for reusing code. Let's take a look at how objects and classes are defined and used in Python. We'll start with this simpleoop. oop stands for Object-oriented programming. We'll make a working copy of this, and we'll go ahead and open the working copy. And let's just take a look at what's in here right now.

This is a class, and a class, in Python, as in many object-oriented paradigms, a class is the definition that is used to create an object. It's sometimes called a blueprint. So objects are instances of classes. So when you take a class, here's the class that we've defined up here, class Fibonacci, and you assign it to a variable, that's called instantiating, or creating an instance, of that class, and the instance itself, in this case, it's just called f, that instance is called an object.

An object is an instance of a class. So the class is the definition, and here we have class, and it's named Fibonacci, and it has these parentheses, and we'll talk later about what goes in the parentheses if anything. Right now nothing needs to go into them. The colon introduces the blocks or suites of code that are indented, and therefore part of the class definition. And here, we have two functions defined as part of this class. And each of these functions, you'll notice that its first argument is self.

And here we have one that's just got self, and here we have one that's got self and some other variables. self is the common name for that first argument. It doesn't have to be self. It's traditionally self. And what it is is a reference to the instantiated object. So when we use the object f, and here we're calling a method or a function inside of the object f, that's what this dot notation does, f.series(). It calls the series function or method, but it calls the copy of it that's part of the object, the instantiated object.

And its first argument is actually a reference to the object f. And so, we can assign and use variables that are encapsulated within that object and that are actually carried around. So if we had different instances of the same class, they would have separate variables, separate values in self.a and self.b. This init function, or method, is actually special. This is called a Constructor.

And this gets called when f gets assigned. So f gets assigned Fibonacci, and that creates an object called f, which is an instance of Fibonacci. As that object is created, init is called, and init is called with a reference to self and with these variables, and here we have 0 and 1. You can define these however you want to because you're writing this constructor. The constructor is optional.

If you don't need it, you don't need to create it. But in this case, we used a constructor so that we could demonstrate how this works. And what this is doing here is it simply creating some default initial values for a and b, which are going to used as part of the Fibonacci series. And then we have this second method, or function, and this one is actually a generator. You see it uses the yield statement. And so that generates an iterator, which is used here in this for loop, and so for r in f.series() is going to give us a series of all the Fibonacci numbers that are less then 100.

So let's go ahead and run it, and there we have our Fibonacci series up to 89, which is the last Fibonacci number that is less than 100. So here we have defined a class called Fibonacci, we have instantiated it, creating an object, called f, and in that object, we called this generator function and we got a series of Fibonacci numbers. So that is a simple example of a class and an object in Python.

Reusing code and data with a class
Video duration: 4m 39s 6h 36m Beginner

Viewers:

Reusing code and data with a class provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training

Subjects:
Developer Web
Software:
Python
Author:
please wait ...