India PyCon 2009
Quick wallpaper changer
Load testing with Grinder
Adding namespace to XML
Opera RSS to OPML
« Javascript: manipulating select boxes
» Pornolize your site
A sample script to show operator overloading in Python. I’m finally beginning to use Python as an object oriented language rather than simple procedural scripting one.
Though this doesn’t do much, same principle can be extended for more complex classes.
"""
Simple example to demonstrate operator overloading
"""
class Money:
def __init__(self,amount=0):
self.amount = amount
def __add__(self, x):
return Money(self.amount + x.amount)
def __sub__(self, x):
return Money(self.amount - x.amount)
def __mul__(self, x):
return Money(self.amount * x.amount)
def __div__(self, x):
return Money(self.amount / x.amount)
if __name__ == '__main__':
mine = Money(30)
print "My money =", mine.amount
hers = Money(10)
print "Her money =", hers.amount
ours = mine + hers
print "Our money (a+b)=", ours.amount
really_mine = mine - hers
print "Really mine to buy beer (a-b)=", really_mine.amount
fantastic_dream = mine * hers
print "Dream money (a*b)=",fantastic_dream.amount
real_life = mine / hers
print "Effective real life money (a/b)=", real_life.amount
Exercise to the reader: extend this so that mine + 20
works!