Methods/Functions

These are blocks of code that perform a task when called.  If you are going to use the same code multiple times it is a good idea to encapsulate it into a method or function.  They can be used as many times as you would like.

Methods and functions are useful for clarity, testability, and re-use.  If used correctly, they will simplify code.

In python, you can create a function like this:

def sampleFunction():
  #here is some code

You can then call the function in your code:

sampleFunction()

Methods and functions can have arguments passed to them.  The method/function has to accept these arguments as parameters.  These parameters can be any type of data, and if there are multiple parameters, each can be different.

Here’s an example of using arguments and parameters:

def sampleFunction(parameter1, parameter2):
    #parameter1 is a string
    #parameter2 is an integer
#calling the function
sampleFunction("This string", 55)

When using methods & functions, you can return a value after running.  You use the return statement for this.  When you call the function in the code, you can pass the returned value into a variable.  Here is an example with some integers:

def sumOf(num1, num2):
    sum = num1 + num2
    return sum
#Calling the function
resultVariable = sumOf(2,4)

In this example, we are passing 2 and 4 as parameters into our function which adds them together and returns the result.  This means that the value of resultVariable will be 6.

Methods and functions are similar.  The reason for the two names is that functions are standalone code blocks, which methods are associated with an object.  Object-oriented programming will be explained more next.

Classes

Classes are a way to bundle data and functionality.  Each class defines a new object type, allowing new instances of that type to be made.  In simpler terms, classes are essentially a template to create objects.

The important thing to note with classes is that python is an object-oriented programming language.  This means python focuses on creating reusable code.  When working with complex projects, classes will help you reuse code, which then will allow you to better scale and maintain your code.

To be honest, classes can get pretty complicated and I could write another 500 words on them, so instead I’ll just put some links here for further reading, especially since I think they explain it better than I would.

For a clear explanation, I’d recommend this blog post about Improving your Python with Classes.

Here is the official python documentation.

Modules

Modules are independent blocks of code that can be reused.  Usually these are used to divide large projects into smaller parts.  This helps make code easier to read.

Modules are packaged into a single file that can work independently or integrate with other code.

Let’s look at an example module.  Basically modules consist of one or more functions, so our example with have 2 functions and be named testModule.py:

def sumOf(num1,num2):
  sum = num1 + num2
  return sum
def productOf(num1,num2):
  product = num1 * num2
  return product

In order to use the module in an application, do the following:

import testModule

testModule.sumOf(2,4)
testModule.productOf(5,8)

You can see the module acts like a library that needs to be imported into the application.  Then you can call the individual functions.