Share this post on:

Python is yet another scripting language. It is easier to learn than Ruby (more human readable). Like all scripting languages, it doesn’t need to be compiled to run. It can be written and executed on the fly.

The most obvious characteristic of Python is it uses indents to indicate structure. Many other programs use closure {}.

Python is an Object Oriented language but it doesn’t need to specify the type of object or method when creating it. It may seem odd at first. But if Java can have something called Generic and check type of object using instanceof, why can’t we make it even more generic and just assume everything is object? Yes, you lose an informing signature, which may be a problem when you are developing an enterprise app which someone else is going to use with absolutely no intention of understanding your what it is. But for a small project, it does save you lots of effort for the initial setup.

In this article I am going to talk about what I like about Python. I am primarily comparing Python to Java. I thought of naming this article Python: the Good Part. But honestly I am new to the Python world. Thus I will leave this title to the experts who are going to write the book.

Polymorphism

  • No need to define super class or interface to use polymorphism

If all of the operations inside a function work with a given type, then the function works with that type.
e.g. the built-in function sum, which adds the elements of a sequence, works as long as the elements of the sequence support addition.
Benefit: You don’t need to create a interface for every class that use by others just to make sure that *if* you have a different implementation in future the others can switch to that.

  • No need of overloading (aka. repeating signitures just for the same thing)

Often, in Java, you need to create several methods with the same name which differ from each other in the type of the input and the output of the function. It is known as overloading.
We need this because we anticipate that users of the method may not want to take the troubles to provide all the parameters needed. And we want to provide default values to save users the troubles.
However, we end up repeating ourselves. Even though we don’t need to repeat the code, we have to create multiple signitures for the same function.
In Python, you only need to write it once.
e.g. inside class Time

def __init__(self,hour=0,minute=0,second=0):
self.hour=hour
self.minute=minute
self.second=second

Now user can call initialize the method with no parameter:
time=Time()
which creates a Time instance with hour, minute, and second all set to 0
Thanks to the optional parameters feature in Python!

  • Note: The first parameter of a method is the object instance itself.

So a method definition
def print_time(time)
is equvilent to a Java defnition
void print_time()
In Java, you use “this” to refer to current instance; In Python, you use the name of the first parameter in your method signiture to refer to it.
Usually you will name the first parameter self.

A String is a sequence
Because why not?
Why do you need a charAt method when you can get a letter in a String just like getting an element in an array?
e.g.
fruit=’banana’
letter = fruit[1]

Why do you need to go through so much trouble to get a sub String or a sub array?
e.g.
s=’Monty Python’
print[6:12]

This prints the 6 to 11 letter.
Now you can forget Arrays.copyOfRange or System.arrayCopy

One more thing about String.

Do you remember how two exactly same Strings don’t equal to each other when you use “==” in Java? A common mistake for newbees as well as everybody who hasn’t gone through enough pain making this mistake.

No more in Python

a=”banana”
b=”banana”
a is b
a==b

Yes, they both return true

Tuples
You can understand Tuples as immutable lists. What does it bring?

  • You don’t need a temporary variable to swap the values of two variables.

e.g.
a, b = b, a
That will work.

  • Return multiple values

Why would you want to compute twice when you can get the quotient and remainder in one short?
e.g.
quot, rem = divmod(7,3)

  • Looping with multiple values

e.g.
t=[(‘a’,0),(‘b’,1),(‘c’,2)]
for letter, number in t:
print number, letter

Oh, yes. You can surely do that in Java. Just define a class with the variables letter and number, create the objects of that class and dump them into the list. It is just a line v.s. a class definition plus multiple object initializations.

  • Zip: you have multiple sequences (String, list, Tuples) and you want to pair the elements together.

e.g.

s=’abc’
t=[1,2,3]
zip(s,t)

It gives you [(‘a’,1),(‘b’,2),(‘c’,3)]

  • A Tuple can also represent an entry in a map.

We just don’t need that many different types of things.

  • You can use a Tuple as a key.

Similar way in Java is to use an object as key. Again, you need to define the class of object first.

Something else

Any program that you can launch from the shell can also be launched from Python using a pipe (an object represents a running program)
e.g.
cmd=’ls -l’
fp=os.popen(cmd)

A functionality very useful for system admin.
Java doesn’t support it probably because it isn’t necessarily running on a system supporting that shell.

 

All in all, Python is a language easy to learn and fast to develop. While enterprises need something as heavy as Java, Python is valuable to those who want things to be done quick. From what I see, Python is becoming a popular language in the Big Data world because of the ease of development and the good libraries for data manipulation and analysis. As Big Data becomes popular, Python is going to be a promising language in future.

FYI, I highly recommend the book Think Python by Allen B. Downey to the beginners.

s11363793

 

Avatar Charlotte Guan

Author: Charlotte Guan

為求功名寒窗度,
迷途辛酸堪回首?
求學求職皆藉口,
只為識聞萬里游。
琴棋書畫皆嘗遍,
心夢隨緣自沉浮。
一朝見得伊人面,
四海八荒任去留。

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.