Skip to main content

Posts

Showing posts with the label python

Some more impressions of Python

List comprehensions: List comprehensions are pretty neat. Before studying Python I did not know what they were. This is a wonderful way to perform some operation on an entire list. Let me explain with an example. Let's say we have a list of numbers and want another list containing the double of all the numbers greater than 5 from the first list. In Java we would have to do something like this: private static List getDoubleOfMembers(List numbers) { List result = new ArrayList (); for(int num : numbers) { if(num > 5) { result.add(num*2); } } return result; } However, in Python we can perform this operation using list comprehension like this: [n*2 for n in numbers n > 5] Isn't this nice? Lambda expressions: Python supports Lambda expressions but it does not support closures like Groovy. I wish Python supported closures. Unchecked exceptions: The blogosphere has had more than it's share of discussions on checked vs. unchecked exceptions. I will not spam y...

My first impressions of Python for the second time

I had worked a bit in Python many years back. Since then I have forgotten almost everything I learned back then. I think the phrase "Out of sight out of mind" applies perfectly to my mind. Since the last few days, I have started relearning Python, and this time I am recording my impressions of Python after having come to it from a Java background. Indentation: Python uses indentation to specify blocks of code, instead of curly braces. I like this, because we anyways indent code to increase readability, so why not achieve two tasks together. Code looks much cleaner without the curly braces. However there may be a little downside. Everyone in the team will have to set up their IDE's in the same way. Things might fall apart if some people use tabs and others use spaces for indentation. Access modifiers: Python does not have public, private, and protected keywords. Everything is public. However, private members can be specified with a leading single underscore. If we use do...