Python - Some syntactic sugar
I’ve been playing with Python for a while now and basically I love it. This post is going to be a very quick overview of some of Pythons features that might be new or a bit odd to a developer from a different background. I’ll be refering to Java, PHP and JavaScript in particular… This isn’t a particular dig at any language, more aiming to highlight a few things that are different in Python.
No curly braces
This is probable the most noticeable difference in Python compared to many other languages. Rather than use { and } to define code blocks indentation is used instead.
-
#Example IF statement
-
if x < y:
-
print ‘x is less than y’#this code is in the if
-
else:
-
print ‘x is not less than y’
-
# while indented all these lines are in the if
-
print ‘Not in the if statement’
I’ve got to admit I didn’t really like this to start with. Then I realised, all python code is really easy to read. We don’t need to rely on developers laying out code properly… it’s forced upon them! In terms of readability, this is one of the best features in python.
(Very powerful) Namespaces
Ok, so I know Java has namespaces, or packages and I know PHP 5.3 has name spaces on the way (no defence for JavaScript here…). However, if you’re ever going to look for an example of namespaces done right… Look at Python. Python’s namespaces are constructed by the folder layout of your application and use the name Package for a folder and module for a .py file). Simple put a __init__.py file in a folder and Python knows it’s a package. The __init__ file is called the first time the package is loaded ever, unless code changes. So you can use it to set something up for example.
So why are python namespaces so good? It’s mostly due to the different ways they can be imported. Also each file holds its own space when used, unlike a require statement in PHP that just mashes everything together… In Python you can import individual methods, classes or even variables from a module.
-
# Grab a few specific items from a namespace…
-
from mynamespace import ClassA, ClassB, MethodA
-
-
# Import a full namespace
-
import mynamespace
-
# or
-
from mynamespace import *
-
-
# Import an give an alias
-
from mynamespace import ClassA as ClassZ
Multiple inheritance
In Python the object model is a bit different, so you can infact perform Multiple Inheritance (You can also achieve this in JavaScript).
-
class myClass (Base1, Base2, Base3):
-
pass
How does this work? Basically Python inherits from them one at a time. So if a method exists both Base1 and Base2 it will only be inherited from Base1. Base2 is then ignored.
Return multiple values from a method
Ok, so Java has its collections and PHP has its fantastic arrays and these could be used to return multiple values. However, Python just makes it easy!
-
def myFunction():
-
return -1, 0, 1
-
-
a, b, c = myFunction();
-
# a now is -1, b is 0 and c is 1
Python automatically creates a tuple and returns the values as one. So you can catch the result of the function as a Tuple, or you can split it up straight away as I did. I’ve found this particularly good when getting co-ordinates, since you always have two variables but how would you return them?

August 21st, 2008 at 12:10 pm
isn’t python sweet?
right now I’m twiddling with python. I love it, just like a very fresh air to me.
=adji=
August 21st, 2008 at 12:35 pm
Definitely, I really enjoy using it. What language background are you coming from?