Murach's Python Programming

More About this Post

1 An introduction to Python programming

  • Python's source code is compiled by an interpreter into bytecode that runs on a Python Virtual Machine - its runtime environment. This virtual environment makes python platform (OS) independent.
  • popular IDE's include PyCharm, Eclipse with pyDev plugin, IDLE shell

4 How to define and use functions and modules


  • prefix function declrations with def
  • Python uses the main() function convention for bootstrapping a program (.py) file. The function is ignored if imported from another module.
#!/usr/bin/env python
hello world docstring
def test():
    print("hello world")

def main():

if __name__ == "__main__":


  • Use 3 double quotes aka docstring for module and function documentation
  • Import statements for a python module named
# imports the file and assigns to a "temperature" namespace
import temperature

# imports functions from Python's built-in math module
import math

# imports the file and sets a defined "temp" namespace
import temperature as temp

# imports a named function which can be called directly
from temperature import function1

# imports the Temperature class
from temperature import Temperature

# imports all named functions that can be called directly
from temperature import *

14 How to define and use your own classes

Class definition conventions

  • Class declaration: class MyClass
  • Constructor: def __init__(self[,args]):
  • Public property: self.publicProp = "public"
  • Private property: self.__privateProp = "private"
  • Public method: def doSomething():
  • Getters and Setters:
class Foo:
    def __init__(self):
        self.__bar = 1
    def bar(self):
        return self.__bar
    def bar(self, value):
        self.__bar = value;

15 How to work with inheritance

Super class

class Foo:
    def __init__(self):
        self.__bar = 1
    def bar():
        return self.__bar

Sub class

class Baz(Foo):
    def __init__(self, Foo):
    def baz():

Instance Checks

from objects import Foo, Baz

def main():
    foo = Foo();
    baz = Baz();
    if isinstance(baz, Foo):
    	print("is Foo")
    	print("not Foo")