Python for Swift developers
Where was all this?
When Margaret Thatcher visited São Paulo in Brazil in 1994, she allegedly exclaimed ‘How did nobody tell me all this existed?’. Which is very accurate, since the city of São Paulo is pretty massive and I do remember the shock I felt the first time I flew over it. These are exactly what my feelings with the Python programming language while discovering it. It was always in my radar, many programmer friends used it thoroughly, even once I was very impressed when my good friend @andyborrell wrote an awesome game in just a couple of weeks using the library pyGame
I even had tried it a bit but not too seriously, but this time I promised myself I would explore the language thoroughly. And it blew me away!
Python allows you a LOT of power with just a few lines of code. A couple of lines of code and you are drawing a UI, with a window and a couple of controls that work in OSX, Linux and Windows. Another few lines of code and you are rendering a game in 2D just like that, etc.
The Python Language
Some people claim that Python and Swift are very much alike. I would say they are quite different, but in a way, Python seems like veeery familiar when writing code. The first difference I found, were the data types Python uses which are: Integer, Floats, Strings, Complex Numbers, Booleans, Tuples, Lists, Dictionaries and Sets.
Apart from complex numbers (which is the first language I see that natively supports them which seems rather odd, since they are not that widely used except for scientists I guess, very cool though) they are all data types which are very familiar for Swift programmers since we use all of those, although maybe a little different.
Python doesn’t use blocks with brackets { } but a very clever way in which the blocks are just inside indentations. In Python indentation is built into the interpreter. Code that’s part of a block must be indented, or else you get an error message. This is one of the features that makes Python specially readably.
Python is very intuitive and using a IDE such as PyCharm Community Edition makes it super simple to debug your code and understand what is going on in your program.
Logo Writer is back!
As a kid in the 1980’s my very first steps in computers and programming where done using Logo Writer, building robots with lego and all that stuff at school. It all seemed almost magical and exciting to me, I would go to the lab every day and try to learn new things.
I have to admit I was a little bit moved when I discovered that Python has a super awesome library called Turtle which basically emulates the Logo Writer turtle I used for drawing when I was growing up.
And this very simple scripts draws this awesome fractal tree:
Classes and Objects
As the modern language it is, Python is an object-oriented programming capable language, and allows its users to create classes and objects.
Output:
- [Johhny, 41] -
theyre the same
- In Python you can create objects just like in Swift. They have a constructor (in some way similar to the Swift one which is described in lines 4 and 5).
- In python you don’t have to declare your object’s member variable (you just access them like in line 4 and they are automatically created i.e. self.name = ‘mortimer’
- In Swift you can override operators! yaay! you can overide ==, +, <, >, *, which ever you want! In this example I overwrote the equality operator
- Just like we did in Objective-C and Swift that you can declare a descriptor method, that prints a friendly description of a class, you can do the same thing in Python:
var description: String { return "-[" + self.name + .. + "]-"}
In line 15 I wrote a simple descriptor for debugging purposes of the class Person which is actually used in line 18.
Making HTTP requests
Making http requests I super straightforward. There is a lib called urllib.request which helps you with the generation of requests, etc. To generate a request you can do the following:
Just like that you are generating requests agains any server available on the internet. Super cool! I haven’t found yet the equivalent of Alamofire, a more comprehensive and complete library for making and handling network requests.
Writing and reading files in Python
Python makes it super simple to read and write files. Here you can find
Be aware that you might have to set some permissions (which are not granted by default) in order to allow your python program to have access to the file system, which of course for security reasons is not grantes by default
chmod +x /Users/nacho/YourProject/yourPythonScript.py
The Python library collection
One of the coolest Python features is its amazing collection of available libraries. You can do almost anything you can do with other programming languages and frameworks like swift. Just to name a few:
- urlib: Allows users to make internet requests
- pyGame: Awesome library to build not-so-simple games
- os: library that gives you Operating System access
- turtle: awesome Logo writter like library