Object Oriented Programming


Overview

Object Oriented Programming (OOP) is a programming language model. It is a conceptual framework that separates data and behaviour. Python has always been an OOP language. A quick run down on terminology and how to implemment OOP in python can be found here. Main principles to investigate include Encapsulation, Data Abstraction, Polymorphism and Inheritance.

Remember everything is an object
Take a look at Writing Idiomatic Python by Jeff Knupp, it is a great place to start learning OOP. Specifically, Improve Your Python: Python Classes and Object Oriented Programming. Another great article can be found at python-course.eu.

When should I use classes?
A class can be described as a blueprint, template or an empty form containing instructions for how to make an object. If you find yourself calling a bunch of different methods on a dataset you should consider classes. That is, encapsulate the data in a class and decide how private this data and associated methods need to be.

Getting started
My first use of OOP was by accident. I wanted to make a user interface for a basic sampling game. I started implementing things like self, special methods, inheritance and composition without knowing what they were and how to deviate from set tutorial information. OOP just seemed like black magic. For example, a basic Tkinter application seemed horribly cryptic without knowing a little OOP.

Examples

GUI using Tkinter:

import Tkinter

class simpleapp_tk(Tkinter.Tk):
    def __init__(self, parent):
        Tkinter.Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        pass 

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()

Mobile apps using Kivy:

from kivy.app import App
from kivy.uix.button import Button

class MyApp(App):
    def build(self):
        return Button(text='Hello World')

if __name__ == '__main__':
    MyApp().run()

Useful resources

results matching ""

    No results matching ""