Categories
PHP Python

Handling Exceptions in PHP and Python

It’s really a very important programming practice to properly handle any possible exceptions… It makes the code more stable, efficient and re-usable.

PHP:
PHP now has a very good exception handling mechanism. Just put a “try” block and then “catch ” the exception if anything goes wrong 🙂

Have a look at the following code:

The code is self explanatory. But you must use the “Exception” word before $e in the catch block to get things going perfectly. Like I did, you can “throw” a new exception in case there was really something unexpected in your code 🙂

Python:
Python is just a fantastic language. Here’s the Python equivalent of the above code snippet:

The only difference in PHP and Python’s exception handling is it’s language structure. In Python, we don’t throw an exception, rather we raise one. Just a bit of politeness! 😉

Conclusion:
Both PHP and Python has very good exception handling techniques. I can’t discuss them all on this blog but you should consult the respective manuals to master them. They are really very essential! 😀

Categories
PHP Python

Why I like Python more than PHP ?

A friend of mine asked me a few days ago, why I like Python more than PHP given that I am more skilled in PHP than in Python.

Well, here is the answer. I like Python because:

– On my machine, Python is faster than PHP most of the time.

– I have to type less when I am building a decently large app.

– PHP is going to be the language of the future, yeah, but still Python has more features to offer.

– Python has a large collection cool libraries and addtional modules. I did visit PECL repo of PHP. I still prefer Python’s module collection.

– Python has a larger built in module collection.

– Python has a very good interactive shell and many decent IDEs. Even the Gedit I use on my Ubuntu has a python console.

– Debugging is pretty easy. Inspection of objects are easier as well.

– Programs can be converted to bytecode.

– Force indentation makes the code more readable.

– Things are always easier and less time consuming on Python. Just compare the BeautifulSoup with PHP’s SimpleXML and DOM. You’ll get the difference.

– Python is well suited for all sorts of development — desktop apps, web apps or even mobile apps.

And I have many more reasons, but what’s the point writing them in? The simplest answer is Python has won my heart and so I prefer it to PHP. Any objections ?

Categories
PHP Python

Calling a function using string

PHP:
In php, you can assign a function name as a string to a variable and call the function by adding “()” after the variable name and appending a semicolon. Like this:

Python:
In python we call the function using the str as a key to the dictionary recieved by calling the globals() function. Like this:

Cool, eh?