Operator overloading in Python

Very recently my friend Hari wrote an article about operator overloading in ruby, and he asked me to implement the same program which he used in his article in python. His aim was to compare the two hottest languages of technology lovers.

Though our example is very simple and straight forward, we consider this as a stepping stone for our comparative study. Below we are giving our example which compares the operator overloading for ‘+’ (addition).
I expect you know about operator overloading. If you dont know, in simple words, it is just like giving special meaning to a language’s operators(‘+’, ‘-‘,’/’,’*’ etc) via new definition though class methods.Read more about operator overloading from here.
In python, operator overloading is achieved via overriding python’s special methods from a python class. If you want to do operator overloading in python you should know about it’s special methods ( which is also called magic methods). Python has a number of special methods, which should be of the form __xxx__. The list is very big, but most commonly used ones are, __add__, __sub__, __repr__, __str__, __len__, __gt__, __lt__ etc. you will get the complete list from here.
lets see how we can give special meaning to __add___. I hope you should be knowing that we cant add two objects, we can add only two numbers. In python you can use ‘+’ to append two strings too. But look how we can add special meaning to ‘+’ to add two objects.
class Add(object):
    def __init__(self, val):
        self.val = val
 
    def __add__(self, obj):
        return self.val + obj.val
 
a = Add(10)
b = Add(20)
 
print a + b
as you expect this will print 30. In order to understand how this works, you can read the print statement like this.  print a.__add__(b)
a = Add(‘Hello’)
b = Add(‘ World’)
print a + b   #This will print ‘Hello World’.
 
The above examples are straight forward use of  ‘+’ operator. We can further extend our __add__ method to intelligently add different data types. Normally python wont allow to add different datatypes. ( try some examples on your python shell)
Here is our extended __add__  method. And see the examples.
class Add(object):
    def __init__(self, val):
        self.val = val
 
    def __add__(self, obj):
        if isinstance(self.val, str) or isinstance(obj.val, str):
            return str(self.val) + str(obj.val)
        return self.val + obj.val
 
a = Add(10)
b = Add(20)
 
print a + b  #30
 
c = Add(‘Hello’)
d = Add(‘ World’)
 
print c + d  # Hello world
 
e = Add(5)
f = Add(‘ is my number.’)
 
print e + f  # 5 is my number
 
In the above definition of __add__ we are checking whether any of the value is a string, if then we will append the two values, otherwise add the values.
Advertisements

Getting details from Youtube URL

We can easily get many details from a youtube url using some of Python libraries. Here am going to show you how we can get
1)Video id
2)Video title
3)Author of video
4)Video thumbnails

We use Python’s urlparse, urllib and simplejson libraries to do this.

import urlparse
import urllib
import simplejson
url_data = urlparse.urlparse("http://www.youtube.com/watch?v=z_AbfPXTKms&NR=1")
video_id = urlparse.parse_qs(url_data.query)["v"][0]  # this is the video id
 
# from this url you will get all data you needed about video, by just replacing the video id
url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video_id 
json = simplejson.load(urllib.urlopen(url))

# this is the title of the video
title = json['entry']['title']['$t']  

# author of video               
author = json['entry']['author'][0]['name']['$t']
print "id:%s\nauthor:%s\ntitle:%s" % (id, author, title)

You will get thumbnails from this url: http://img.youtube.com/vi/video_id/image_id.jpg
image_id can be 0,1,2 or 3.