PHP To Python

Reading time ~1 minute

Lessons learned going from PHP to Python.

Classes

While Python does have and use classes, it’s not always required to use them. You can think of files as classes in most cases.

File: cat.py

def meow():
	print "meow"

File: speak.py

import cat

cat.meow()

Although, if you want to use classes you can. There are some interesting differences to remember.

class Cat: # Class names are StudlyCased
	def purr(self): #functions are typically lowercase, need self  if you aren't passing params
		print "purr"

var kitten = Cat() # To instantiate use parens () "new" is not a thing in python
kitten.purr()

What is self? Check out this answer here

What about construction?

PHP:

<?
	class Kitten() {
		public function __construct() {}
	}

Python:

class Kitten:
	__init__():
		# construction stuff here

Switches

PHP:

<?
switch ($i) {
	case 0:
		echo "i equals 0";
		break;
	case 1:
		echo "i equals 1";
		break;
	default:
		echo "default";
		break;
}

Python:

i = 1
if (i == 0):
	print "One"
elif (i == 1):
	print "Two"
else:
	print "default"

Notice in Python it’s just an if-elseif-else statement. In Python there is no switch statement. A switch is just shorthand for conditionals, so it’s not a huge loss if you really think about it.

Notice

This article is a work in progress. More to come. If you notice any issues or typos, please leave it in the comments and I will happily fix it.

Quick Laravel/Lumen install with Docker!

The default installer that comes with Laravel is great, but unfortunately that means you have to install dependencies of PHP and Laravel/...… Continue reading

Aftertouch in Logic Pro X and 2015 Macbook Pro

Published on February 24, 2016

AWS Nginx Proxy for blog subdomain.

Published on February 25, 2015