Recovered from the Archives - From PHP to Python

I've been a PHP developer for the majority of my professional career. While I did my fair share of ActionScript (dark, dark days) and even ASP (darker, darker days), I stuck with trusty ol' PHP. I called it my Personal Happy Place. I didn't really - that would be stupid.... I vowed I would never leave its side - PHP and I were in love.

But one day, off in the distance, came a language. A language so clean and elegant I couldn't keep my eyes off it. Its beautiful syntax flowed through elegant dark color schemes, and its slim syntax line was breathtak........ Sorry, what was I writing about? Right, I thought I would write a post outlining some of the differences that stood out to me during my switch.

Semi-colon, semi-colon. Where for art thou semi-colon?

To continue with the seriously creepy relationship analogy: The semi-colon is like that inside joke you shared with a ex-girlfriend that you keep referencing during a fancy dinner. You think the girl across from you should know about it, but instead, she just blankly stares at you as you laugh hysterically. Slowly, you become quiet when you realize you're actually kind of a douche. If it weren't for the colon, I'd suggest you destroy the key all together - I've sheepishly deleted the semi-colon way too many times to count

PHP

echo "oh PHP, I'm always finishing your sentences";  

Python

print "Pfft, Aren't you done your sentences when you stop talking?"  

White-space Aware

This honestly scared me a bit when switching to Python. To set this one up for all you non coders - In PHP (and most languages), blocks of code were defined by cuddling your code in between two security brackets.

if (candy_mountain == false){  
    echo 'Shun the non-believer'
}

Python figures you'll probably be indenting the code anyway, so why the redundant brackets? Anything indented will be considered part of the statement.

if (candy_mountain is False):  
    print 'Shun the non-believer! Shunnnn'</code>

Like I mentioned above, when I first read that Python used tabs to block out code I was pretty nervous. This was due to my last brush with a white-space aware language... COBOL. (see: unspeakable horror)

COBOL is a language developed in the 60s, for the sole purpose of terrorizing me through college. I'm fairly certain the dean was violently forced into keeping it around until it had a chance to destroy me. It was removed from the curriculum the following year...

COBOL Stupidity

01 StudentDetails.  
    02 StudentId        PIC 9(7). 
    02 StudentName. 
        03 FirstName     PIC X(10).
        03 MiddleInitial PIC X. 
        03 Surname       PIC X(15).
        03 Stupidness    PIC X(>9000).</code>

Despite my initial fear, I was pleasantly surprised with what Python can achieve by enforcing this. The by-product is clear, legible code all while keeping keystrokes down. As an added bonus, it stops people from trying to get all creative with their code formatting.

// totally valid PHP
if (thisSyntaxIsStupid)  
{
$my    = "variables";
$like  = "to";
$be    = "together";
$like  = "idiots";
                    if (MahSyntaxOverHurr){
                            Durrrrrrrrpahdurp();
                }
    }

And for the record, you think I'm over exaggerating as to how stupid COBOL was? Edsger Dijkstra, a renowned computer scientist, even went as far to say this:

The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offence. Edsger Dijkstra

I quite like this man.

Wait, I have to import this function myself?!

Let me put this into terms for all you stereotype-isters out there.

You're in your mothers basement (see: your room) putting the finishing touches on her cat accessory storefront. (...In between WoW raids, naturally.) Suddenly you get a gnarly craving for some smokey bacon hot pockets. So, you do what any self-respecting twenty-something would do, and announce your craving at the top of your lungs. Three minutes and a microwave ding later, you are graced with a couple of steaming hot happiness rolls - hand delivered to you by the women who grunted you into this world. (With a bonus fruit punch capri-sun on the side - she totally gets you.)

Sounds like a pretty sweet deal eh? WRONG! It's called lazyness, and it'll make your programs fat! If you try that crap with python, it'll smack a syntax error in your face. And it won't just say "Get your own hot pocket, mister!". No no, not python. It'll get all cute with you and be like "Hot pocket? I've never heard of these before? You must not have invented them yet". Python will beat the lazy right off your backside.

import datetime

#yup, even datetime functions aren't included
datetime.today()  

There were a few other gotchas when I initially made the switch What the heck is a tuple?!, and I could honestly go into a full detail on each one of these in their own article, but I think I've exhausted your patience enough for one post. If you take anything out of this though, let it be this:

Give a man a function, and he's quicker for the night. Make a man get his own hot pocket, and he's confused as to how this phrase works here.

comments powered by Disqus