your code


In today’s world of software development, one can easily lose themselves in a sea of complexity.

Amongst all this complexity, it can be challenging to find your essential pieces of code.

The essential part of your code is those few lines that capture the true work of what you’re trying to accomplish, buried under all those layers.

On top of that, in Java, one can still have even more difficulty in trying to cut through the ceremony¬†to get to the essence of your code, whereas in Python, I’ll assert, one gets to the essence of your code more easily and efficiently.

One can start to see the ceremony in Java in the following example:

Let’s say you have a list of stocks (with following attributes: ticker, open price, last traded price) that you’d
like to sort in-memory.

In Java, one standard way of doing this is to:

1. Create a comparator

public static void sortByLastTrade(List<Stock> portfolio) {
  Comparator<Stock> c = new Comparator<Stock>() {
    public int compare(Stock o1, Stock o2) {
      return o1.getLastTrade() - o2.getLastTrade();

2. use java.util.Collections.sort() to perform the sort with the created compartor above in #1

  Collections.sort(portfolio, c);

Pretty straightforward.

Alas, if you suddenly wanted to sort on another attribute, you’d have to copy the comparator in #1 and modify it like this:

public static void sortByOpen(List<Stock> portfolio) {
  Comparator<Stock> c = new Comparator<Stock>() {
    public int compare(Stock o1, Stock o2) {
      return o1.getOpen() - o2.getOpen();

You can smell the boilerplate. Java is forcing ceremony on us. I just want to sort!

Here’s what Python has to say:

>>> portfolio_tuple = [ ('APPL', 203.25, 204.50), ('MSFT', 29.12, 29.08), ('ORCL', 21.90, 21.83) ]

>>> sorted(portfolio_tuple, key=lambda stock: stock[1]) # sort by last Trade
[('ORCL', 21.90, 21.83), ('MSFT', 29.12, 29.08), ('APPL', 203.25, 204.50)]

>>> sorted(portfolio_tuple, key=lambda stock: stock[2]) # sort by last Open
[('ORCL', 21.90, 21.83), ('MSFT', 29.12, 29.08), ('APPL', 203.25, 204.50)]

I think it safe to say that the python example gets to the essence of your code better than the java one.

* A tuple is heterogeneous, immutable list
** I like to think of lambda as function lite


In Java, one could potentially write all of their code in one single line. (Thankfully, no one does this because it would make for illegible code).
Programmers naturally indent to logically order their code.

So, Python argues, if you’re already doing this indentation, let’s enforce this as a feature of the language.
With this “feature” comes great responsibility. Hence, the PEP 8 Style guide for Python.

  • 4 spaces per indentation level
  • No hard tabs
  • Never mix tabs and spaces
  • One blank line between functions
  • Two blank lines between classes

Don’t worry Java-friends, it will take you all of 1.5 hours to get over this.


Python is an interpreted language. In short, this means the extra step of compiling your code is removed.
With this omission you can execute the Python interpreter directly on a file with python code in it.
(normally suffixed with the .py extension).



Or, you can work in an interactive Read Evaluate Print Loop shell. You can initiate this interpretive
shell by calling Python directly

>>> s = 'now we are inside the REPL'
>>> s
'now we are inside the REPL'
>>> exit()

The REPL allows for quick and easy experimentation.
(Think of the tedious steps you’d have to go through in Java to write a simple program!)


# defining a list
beatles = ['john', 'paul', 'george', 'ringo']

# looping over a list
for member in beatles:
  print member

# in conditional
if 'paul' in beatles:
  print 'list contains', 'paul'

# compare against values, not references
if beatles == ['mick', 'keith']:

# defining a dictionary
also_known_as_map = {'singer':'john', 'bass':'paul', 'guitar':'george', 'drummer':'ringo'}

# grabbing a value based on key


>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
>>> [fruit.strip() for fruit in freshfruit]
['banana', 'loganberry', 'passion fruit']

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.iteritems():
... print k, v
gallahad the pure
robin the brave


import urllib
import httplib
from urlparse import urlparse

class PieService:

  def __init__(self):
    self._headers = {"Content-type": "application/x-www-form-urlencoded"}

  def flush_cache(self,url):
    params = {}
    return self.make_request(url + '/flushcache', params, "GET" )

  def start_transfer(self,url,domain,source,service_code,data):
    params = { 'Domain': domain, 'Source': source, 'ServiceCode': service_code, 'Data': data }
    return self.make_request(url + '/start', params, "POST")

>>> my_pie = PieService()
>>> my_pie.flush_cache( 'http://localhost:45601/pieshell/' )


Sick of writing static methods to perform ‘utilities’?

>>> len([1,2,3])
>>> len('dave park is cool')

>>> sum([1,2,3,4,5,6,7,8,9,10])

>>> help(sum)
Help on built-in function sum in module __builtin__:
sum(sequence[, start]) -> value

Returns the sum of a sequence of numbers (NOT strings) plus the value
of parameter 'start' (which defaults to 0). When the sequence is
empty, returns start.


* Points wiki page –
* Official Python Docs –
* Points mailing list –


This presentation was inspired by Howard Lewis Ship’s presentation on Clojure

Shares 0