In this video, learn how to get started writing programs in Python using the Eclipse IDE with the PyDev plugin. This gives you a foundation for all Python projects you'll develop in this video series.
- [Instructor] Hello, and welcome to Python Projects. In this video, we will get started with using Python 3.5. We will start in a procedural, function-based programming style, and after that, we will move forward to an object oriented programming style using Python. In Python, we can start simple, by simply printing out a print statement.
We now click the run button, it will execute the print statement, and we can see at the bottom of the Eclipse console, our string of the print statement has been printed out. "Hello Python". We can achieve the same thing by putting our print statement into a function, and then of course, we have to call the function. So we now execute our small Python program, clicking run. When run, we get the same result down here. "Hello Python".
The next step is to turn our functional programming into OOP, into object oriented programming. We do this by defining a class. We put our method into this class, and bind it to the class by using the self keyword. Then we have to create an instance of our new class, and we call the method on the instance of the class. When we now execute our program, we get the same result, "Hello Python", but it's object oriented.
In Python programming, we typically add this line of code to the end of almost every module. If the module gets imported, everything that follows the "if double underscore name equals quote double underscore main" code will not get executed. If it's run by itself, it will run, and this is a great way to test, or self-test each module. In Python, we will typically move the creation of the class of the code to the method of the class down into the "if double underscore name" section.
So, when we self-test it, that's what we going to run. The Python language is based on indentation. If, for example, we move the print statement up to the left, without the proper spacing, and then when we try to execute the program, we'll get an error, which says "indentation error". So that is basically a syntax error. The language won't run. It's a mistake.
So far, programming in OOP hasn't given us much advantage over functional programming. Things start to get more interesting when we add this "def double underscore init" method to our class. Then, when we create an instance of the class, which we do down here, we can pass in a string. This string is the second argument, the first argument is self, so self is being ignored, because it binds the method to the class, but this one where we passing in here the string, becomes passed to the msg variable, and this one gets assigned to self message.
Now when we call "Say Hello", it's going to use the message that was passed in to the constructor, or the interpreter in this case, at the construction of the class. When we now run our Python program, we still get the same result. That is a good thing. Another thing we can do is to move the call to "Say Hello" into the init method. So, down here, when we create the instance of the class, only create the instance, we not really use it, but when we execute the program, we still get the same result.
Which prints out "hi", coming from here, and by creating the instance of the class, it automatically calls the "double underscore init" method, within the init method we call the "Say Hello" method, we use self, and it prints out the same result. For this little code snippet, might not show too much, this truly is the beginning of object oriented programming using Python. As we have seen previously, we can program using Python in a functional way, in a simple way, now in an object oriented way, and we can mix and match all of those different paradigms.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Running Python programs within Eclipse
- Setting breakpoints
- Using the PyDev debugger
- Using design patterns
- Creating a GUI application with Tkinter
- Writing a Windows scheduling service