Welcome to

Mimpi Development

Developing for tomorrow, one dream at a time

Objects and Functions

Posted on 15 Apr 2019 in Blog | 0 comments

Now for some fun. Everything in Python, from Functions, Classes and strings are objects in Python. This means, a few fun things:

  1. They all have types, can be passed as function arguments and can have methods and properties.
  2. You can easily override functions in objects, where the function doesn’t quite work how you need/want it to work you can write your own definition and replace it.

With web applications, writing functions with as little context, and side-effects as possible is the smarter way to go, as running multiple instances of a python process can lead to concurrency problems, as an object may be deleted before another object goes to read it. You know.. chicken and egg syndrome. Isolating functions with context data, from functions with logic can have the following benefits:

  1. Pure functions are deterministic – Given a fixed input, the output will always be the sam.
  2. Pure functions are easier to change or replace
  3. Pure functions are easy to test
  4. Pure functions are easy to use.

Defining a function

def function_name():
print("My function")

function_name()

The above code snippet creates a function and calls it. So in Python land, indentation is master. When you define your function name, end that line with a colon (:) and then the next lines that define the function need to be indented. Once the function code has ended remove the indentation and continue.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.