python, a closer look

A while back I posted a short post about Python, and simply stated that I liked it. Looking back it was a very fanboy-esque post, rather than a post that actually explored the reasons why I like the language. In this post I intend to explain my feelings a little more in depth. Also, I apologize, this is going to be a long post with lots of code.

Style

Python is built from the philosophy that code should be readable and re-usable. This philosophy is built right into the syntax of the language, some of which may be uncomfortable at first to programmers that are used to C-style syntax.

  1. Lack of curly braces and semi-colons
  2. White space matters

Here is some example code. This is a small function that I wrote for an algorithm challenge a few months back. This function takes in 2 integers, x and y, and returns x^y % 10, or in other words, if you raised x to the power of y, what would be the last digit.

It takes some getting used to, but once you realize that blocks of code are separated by how much they are tabbed, in other words, everything that is lined up is in that block, then you can look at the code and instantly tell what is in which block. But Ike, you might say, you can do that anyways in Java (or C or C++). Yes, you can, however, you can also not, and get away with it. In those languages, blocks are separated by curly braces ‘{‘ and ‘}’, and you can get away with having everything lined up on the left side. Python forces you to write readable code, also, everyone writes code that looks the same, so you don’t have to take the time to either adjust your mind or re-stylize their code before you can read it. (For example, where do you put the opening bracket? On the same line or the next line? If you do it differently than I do, it takes me a moment to understand your code.)

Strong Dynamic Typing

Python features strong dynamic typing, meaning that variables can be assigned and reassigned to any type, however, they cannot be implicitly coerced into another variable type. So, because it is dynamically typed the following is a legal, where you first declare a variable as an int, and then change it to a string later.

Now, this in and of itself is not useful, and to some may even seem like a bad feature. However, the following example is where this does become useful:

This allows us to convert it into an int without the need for another temporary variable. We can even parse the string first, pulling out only digits, and then converting them to an integer. This saves us the use of another variable, and we can give our one variable a good, meaningful name.

As for strong typing, this simply means we can’t do something like the following:

Because Python is strongly typed we can’t coerce y into a string ans then concatenate it to x, we would need to explicitly convert it to a string first, then concatenate (or convert x into an int and add them, if that is the intention.)

Tuples and Lists

This for me is one of the best features of Python, at least of the ones I’m going to cover in this post. Tuples and Lists are very similar, they are containers that contain items. The difference is that tuples are immutable, they cannot be altered after they are created, and lists are mutable, they can be altered. There are other powerful container types built into Python as well, but in this post I am only talking about these two.

Both lists and tuples can contain elements of non-matching types. So a list can contain a string, an int a double and an object of some kind, same with a tuple.

Unpacking variables from lists and tuples is an incredibly useful feature as well. You can assign multiple variables at once to receive the contents of the list or tuple, dumping them out in one go. Now, here is where it is the most useful: Returning multiple outputs from a function.

When I first learned Java, I was annoyed by the fact that I couldn’t return multiple things from a function, and there were times when I needed to. In order to do something to that effect I would need to make a special class just for holding those data pieces and then return an object. Now, in Python, we’re basically doing the same thing, except there is less overhead if we use a tuple, and we can automatically unpack it on the other end.

And finally for lists, one of my favorite features of lists is list comprehensions. The following code returns a list of all the squares between 1 and 100 (inclusive)

This is equivalent to:

Dictionaries

I lied. There is one other container that I will talk about. Dictionaries, (in Python they’re also called dicts). This one built in container pretty much nullifies all need for pure containment classes. You know the ones I’m talking about:

The little classes that are used for nothing more than pairing stuff together. In my opinion, unless you are defining specific methods to be done with that data, there should be no need for a class like this. With python there isn’t.

Now, if we want to access and change, say, the address number:

And there we have it. This is a pretty trivial example, as there are dictionaries that exist in other languages as well. However, because of the dynamic typing of Python, we aren’t restricted by types. We don’t have to have a dictionary that maps strings to strings, it can map anything to anything.

They are incredibly powerful containers built right into the language, allowing you to do so much. Python is still an object oriented language, and should you need to actually need to store methods with your data you can always make a class. However, with dictionaries, as I mentioned, you don’t need to create arbitrary classes that do nothing more than store data with getters and setters.

Conclusion

Python is awesome.

This is by no means a full, complete tour of the features of the language. Instead, it’s just a taste of a few of the features that I personally found to be helpful, useful, interesting and awesome about the language. If you are interested in learning more, the python documentation is a good place to start. (I personally use Python 2.7 still, and all my code examples are in 2.7 syntax.)

games for adults

“It seems that games are perceived to be for kids and should never tackle themes a Saturday morning cartoon wouldn’t. It would be pretty depressing and limiting if all developers accepted that as the status quo. Our games are being played by intelligent adults, so there is no reason why we can’t treat them as such.”

-Dominic Matthews (Ninja Theory)

In this article, Matthews makes an excellent point. What a lot of people don’t realize (or at the very least my parents) is that the average gamer is 30 years old. More and more adults are playing video games, an more and more games have the opportunity to present their point to an older and older audience. Since more and more games are being marketed to adults, parents need to make sure they are aware of who the target audience is of the games their kids are buying. Games (just like movies and books) that are aimed at intelligent, responsible adults are not meant to be played by children.