Learntofish's Blog

A blog about math, physics and computer science

Tutorial: Object Oriented Programming in Python – Part 10

Posted by Ed on December 26, 2011

This is the continuation of the Python OOP tutorial. Today I want to describe how you can use Python in Eclipse by installing PyDev. Here, I will assume you have Eclipse installed and some familiarity with it.

Eclipse is a wonderful IDE known to Java users. I like it because of its auto-completion feature, e.g. if you use the dot operator on an object, Eclipse will show you the available methods. You can use Python in Eclipse by installing PyDev. To install PyDev follow the instructions here: Installing PyDev Manual. Or watch this youtube video.

1. Create a project (follow the instructions there)
2. Create a module (follow the instructions there)
3. In example.py paste the following code:

class Airplane:
    __fuel = 0
    maxFuel = 24000
    
    def addFuel(self, volume):
        if(self.__fuel + volume <= 24000):
            self.__fuel = self.__fuel + volume
        else:
            print("Error: You are adding too much fuel!")
            print("Fuelling not possible!")
        self.__printStatus()

    def __printStatus(self):
        print("Current fuel:", self.__fuel)

That’s the code from the last tutorial (module aircraft.py). Now, we don’t want to use the Python-Shell but instead write our commands in a function myProgram() (see part 6).

So, add the following to example.py (add lines 17-22):

class Airplane:
    __fuel = 0
    maxFuel = 24000
    
    def addFuel(self, volume):
        if(self.__fuel + volume <= 24000):
            self.__fuel = self.__fuel + volume
        else:
            print("Error: You are adding too much fuel!")
            print("Fuelling not possible!")
        self.__printStatus()

    def __printStatus(self):
        print("Current fuel:", self.__fuel)


def myProgram():
    airbus = Airplane()
    airbus.addFuel(20000)
    
if __name__ == '__main__':
    myProgram()

– Notice what happens when you type “airbus.”. Eclipse recognizes that you want to use the dot operator. It will then show you the attributes and methods of the object.
– Choose the addFuel method and press ENTER. This is called auto-completion which means that Eclipse completed the code for you.
– Finally, run the program (follow instructions there). If you’ve done everything correctly the console output should read:

Current fuel: 20000

Note:
There is also an auto-completion in IDLE. You can test it in IDLE by running the aircraft.py module from last time in the Python-Shell (use F5). Then type the following in the Python-Shell:

>>> a = Airplane()
>>> a.

– Don’t press ENTER after the dot!
– Now, press CTRL+SPACE. The options addFuel and maxFuel should appear. You can choose one of them by pressing up or down. Choose addFuel.
– Don’t press ENTER! Instead press the bracket ( key. IDLE will then auto-complete the code for you, i.e. it will paste addFuel( for you. The result should be:

>>> a.addFuel(

– Finally, type 20000) and press ENTER.
– So, by using CTRL+SPACE you didn’t have to type addFuel(.
However, using CTRL+SPACE can crash IDLE (I encountered this in a Linux version). Just try if it works for you.
– In IDLE, I couldn’t use the auto-completion outside the Python-Shell as in Eclipse. I noticed this when I wrote the myProgram() function in IDLE and no auto-complete was available for the object airbus. (If auto-completion for objects outside of the Python-Shell is possible please write a comment below and let me know)

Note2:
The only thing that I miss in PyDev/Eclipse is running a file (e.g. example.py or aircraft.py) in a Python-Shell. In IDLE you can simply press F5. However, in PyDev I have not yet discovered such a feature (in case I missed the feature let me know).

References:
Official PyDev Getting Started Guide

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: