Join Bill Weinman for an in-depth discussion in this video Reusing code and data with a class, part of Python 3 Essential Training.
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.
- A Python 3 quick start for experienced developers
- Creating functions and objects
- Using Python's built-in objects and classes
- Repeating code with loops and iterators
- Understanding and using conditional expressions
- Creating sequences with generators
- Reusing code with objects and libraries
- Handling errors with exceptions
Skill Level Beginner
Q: The installation process for the PyDev Eclipse plug-in doesn’t work as described in the videos. What should I do?
A: The version of Eclipse used in the recording of Python 3 Essential Training was Eclipse 3.5.2 (Galileo SR2) and the version of PyDev was 1.5.7. Soon after recording, the Eclipse project was updated to version 3.6, called Helios. As of this writing, the current version of Eclipse is 3.6.1 (Helios SR1). <br /> Around the same time as these updates were being released, the PyDev project was updated as well. As of this writing, the current version of PyDev is 1.6.2. If you are using these most recent versions, the procedure for installing Eclipse itself has not changed, but the process documented in the videos for installing PyDev may not work. <br /> <br /> We will be releasing new versions of the videos soon, but the author has posted a document describing the new installation procedure at:<br /> <a target="_blank" href="http://python.bw.org/pydev-installation/">http://python.bw.org/pydev-installation/</a>
Q: How do you install the pydev-interpreter in Eclipse Indigo on Mac OS X Lion? It's significantly different than what's shown in the video "Installing Python 3 and Eclipse for Mac."
A: Since this course was publishing, there have been upgrades to Mac OS X and Eclipse. In this scenario, after installing Python and Eclipse and the Pydev interpreter, there is a different directory to go to when modifying the preferences. As instructed in the movie (at around 6:40) restart Eclipse and then go to Eclipse > Preferences and drill down to Pydev > Interpreter - Python in the sidebar. Click New and in the Select interpreter dialog that opens, click Browse.<br /><br />The Open dialog box will open, but does not appear to display your hard drive. You must press and hold the Command+Shift+Period keys. This will display all hidden files on your system. Navigate to the new path ~/Library/Frameworks/Python.framework/versions/3.2/bin/python 3.2 and click Open.<br /><br />You should be able to proceed normally from there.
Q: When I try to install PyDev, it's not showing up in the Available Software window.
<div id="pastingspan1">A: This can happen if the pydev.org/updates site is down.</div><div> </div> <div id="pastingspan1"></div> <div id="pastingspan1"> You can manually download PyDev from the pydev.org web site, or from my site here:</div><div> </div> <div id="pastingspan1"></div> <div id="pastingspan1"> http://python.bw.org/</div><div> </div> <div id="pastingspan1"></div> <div id="pastingspan1"> Installation is simple. I've included instructions on my web site above.</div>