Daily Archives: August 5, 2008

Today: My Favourite Feature of Python (at least for today)

I have been doing object oriented stuff before the word was coined. I have tried all the languages (except LISP) and each has bits to like. But the feature where I can manufacture a return object on the fly is simply too awsome for words!

It completes Object orientation – and yes I know – it was stolen from / homage to LISP. OK – I admit is LISP was right on 5% of its language.

Here is my snippet – a silly bit of code.

def addandsub(a,b) :
return (a + b, a - b)
print "Hello"
(x,y)  = addsub(10, 4)
print x,y

I returned a tuple with two integers – it solves the sucky single primitive return value of Java and C++ which both inherited from C.

Perhaps this is the strength of Python – it inherits from C, PERL, and LISP and mixes them nicely to make a new life form with superior hybrid genetics. HURRAY!

Update: Thanks to Stuart Feeeman from Georgia Tech, the right way to make an empty block is to use the “pass” statement.

Ok – while I am gushing – I should explain what bothers me the most is the fact I cannot make an empty bracket like this

if x < y :
# Do nothing here
else:
print "Hello"

I want to have the first part of the brace to be empty - but nooooo - I can't do that. I have to do this:

if x < y :
pass # I love an explicit no-op in a language
else:
print "Hello"

Thanks Stuart!