Python Easter Eggs

This is something I discovered incidentally... On the Python interpreter try out these commands C:\Python27\pylibrary\PyLibrary>python ActivePython (ActiveState Software Inc.) based on Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import this  --> Prints out Zen of Python The … Continue reading Python Easter Eggs

Overloading in Python

Some say Python doesn’t allow overloading of methods or constructors. Well, they are right in some way!  A coding example below, [sourcecode language="python"] def add(a,b): return a+b def add(a,b,c): return a+b+c print add(4,5) [/sourcecode] If you try to run the above piece of code, you get an error stating, “TypeError: add() takes exactly 3 arguments … Continue reading Overloading in Python

Python Decorators

Let's start with something simple..What is a decorator? According to, "A decorator is the name used for a software design pattern. Decorators dynamically alter the functionality of a function, method, or class without having to directly use subclasses or change the source code of the function being decorated" A classic example tat I can … Continue reading Python Decorators

Mystery with remove and pop in Python lists

Thanks Vishal for his inputs on this.. According to python docs list.remove(x)Remove the first item from the list whose value is x. It is an error if there is no such item.list.pop([i])Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last … Continue reading Mystery with remove and pop in Python lists

Python: Making objects callable

Problem Statement Have you ever wandered if we could make an object callable? Yes, I mean just use the object name as if you were calling function! Intersted? Solution [sourcecode language="python"] class Add: def __init__(self, num1, num2): self.num1 = num1 self.num2 = num2 print "Sum of", self.num1,"and",self.num2, "is:" def __call__(self): return (self.num1 + self.num2) add … Continue reading Python: Making objects callable