A few recommended books, movies, games, and albums. If you want to look for more recommendations, feel free to look at the larger selection over at Amazon or my Amazon Store with more recommendations.

  • Cryptonomicon
    by Neal Stephenson
  • DreamCypher
    Dancing Ferret
  • Tron: Legacy (Amazon MP3 Exclusive Version) [+Digital Booklet]
    Tron: Legacy (Amazon MP3 Exclusive Version) [+Digital Booklet]
    Walt Disney Records
  • The Moon Is a Harsh Mistress
    The Moon Is a Harsh Mistress
    by Robert A. Heinlein

Entries in python (2)


Python Variables

As much as I love Python, there are two things about it I’ve seen drive programmers experienced in other languages absolutely nuts. The first is the syntax. Instead of using curly braces to delimit blocks of code, Python uses indentation. The second is how variables are handled.

But wait! Aren’t variables like boxes with names, that we can put data into using the names?

Well, yes, and barring pointer operations in C and its derivative languages, that mental model often works perfectly well. For most languages.

So what makes Python different? First, the fact that everything is an object. Even basic datatypes like integers. 

Second, variables in Python, like PHP, are dynamic. The type of data is not predetermined when you first create the variable name, or “identifier”. Because of this, you will never see a variable declaration specifying a type as in Java or C like this:

int car = 3;

As a result, the following commands are perfectly valid:

car = 3

car = “Ferrari” 

Third, and most importantly, the variable identifier is just a reference to the data. Any data. Even integers. For example, if I create the variables “car” and “pet”, and set them both to 3, they both point to an integer object with the value of 3. If I set the variable “joe” equal to “car”, it doesn’t make a copy of “joe” - it just points “joe” to the same object as “car”: 

For immutable data types like numbers and strings, the behavior is effectively the same as you’re used to in any other language, because if you change the value that a variable points to, it just gets redirected to the new value. For example: 

Where things get interesting though, is when dealing with mutable data, like lists. 

Try this out: 

L1 = [2,3,4]

L2 = L1

L1[0] = 24

In the first two lines, you create a list, and set the second variable, L2, equal to the first. Since we are just passing pointers, then L2 actually ends up pointing to the very same list as L1.

So when, in the third line, we set the first item in the list L1 to 24.

Since both L1 and L2 point to the same list, the first value in BOTH lists is now 24.

Why was it chosen to make Python behave like this? I haven’t found the specific answer in either the sites, or at Guido Rossum’s blog detailing the history and philosophy behind it, but It’s almost certainly a part of trying to make everything an object, while allowing dynamic variable types, and reducing the memory overhead as much as possible while also keeping you from having to deal with memory management.  

One of the most useful consequences of these design choices is the ridiculous amount of flexibility in how you pass around and manipulate data. Since everything is an "object" - you are not restricted to "integers" and 'floats." Specifically, I’m talking about “duck typing” - named for the expression “If it walks like a duck, and talks like a duck.”. In the case of Python, (or id-type objects in Objective-C), what this means is that not only can you assign any type of data to a variable as we already discussed, but you can try to run any method on it you wish as long as the object supports the method. 

This is fundamental. Unlike, for example, Java - where polymorphism is limited to methods of a class or superclass from which the class inherits - Python doesn’t care what type the class is - it just cares if the method exists. It doesn’t matter whether it’s a duck, a rectangle, or a car, it just matters if it responds to the method “quack()”. 

So how do we deal with this?

Well, if you’re just passing the variable into a function to be read, it doesn’t matter. If, on the other hand, you want to actually make a copy, or create a new, modified data set without changing the original, you need to make a copy. 

Even with copies we run into issues. Lists are objects just like anything else. What happens if the list you are copying contains lists? A simple copy of the top-level list makes a separate set of references for anything in the list, so now both lists seem to be entirely separate. Yet, if the list we are copying has sublists, our copy still contains shared pointers to those sublists, with all of the problems we saw above.

So first, unless you, the programmer explicitly ask it to, Python only passes references. This is never going to cause trouble when dealing with immutable data types. 

Secondly, if you want to make a separate copy of a list or mutable datatype, you have to explicitly copy it by using the “copy” method, or a slice  like this:

aList = [0,1,2,3]

bList = a[:] # shallow copy of a using a slice


import copy #import the copy module

aList = [0,1,2,3]

bList = copy.copy(aList) # shallow copy using copy module

Third - if you have sublists or other mutable data types within a mutable data type, you need to make “deep” copies. Deep copies allow you to make separate copies of any sublists, dictionaries, or other mutable data that would cause issues in a shallow copy. They require you to either code your own method, or again, you can import the copy module, and use the deepcopy method. 

import copy #import the copy module

aList = [0,1,2,3, [“a”, “b”, “c”]  ] # list with sublist

bList = copy.deepcopy(aList) # deep copy using copy module

Hopefully this helps someone out there trying to get their heads wrapped around Python variable handling. 


Learning to Program

Programming is an art form where we ask a question, and then write out very detailed instructions on how to answer that question. The instructions are written in a very limited subset of words, with a very precisely defined grammar. From this foundation, we can eventually end up at Microsoft Office or the latest XBox game.

Of course, there is a long, long road from writing out your first “hello world” tutorial to the latest word processor or Portal adventure from Valve. On this road, the byways will include a bewildering array of languages, options, tools, and a seemingly insurmountable mountain of functions, libraries, and bizarre vocabulary you’ve never seen before.

It’s very easy to get discouraged if you allow yourself to. The trick is to break the process down into chunks that you can manage, that give you some feedback, that let you go “Yes, I succeeded!” so you don’t feel like you’re churning away without making any progress whatsoever. This comes under the header of “Choosing your projects wisely.”

Actually, the first trick is to remember that trying to program gets you nowhere. You’ve actually got to do it.

What Have I gotten Myself Into?

A challenge. Like all worthwhile challenges, it will not be easy. The good news? You don’t have to invest a lot of time or money to get started. So first choose a goal - a mid-term one. 

What are you interested in creating? An iPhone app? An android app? A web-based app? The next Twitter? That will determine your starting point, your choice of starting languages, and what tools you need to pick up.

The path is not straight.

Keep in mind that programming, like math, depends on you learning concepts and then building on them. Programming as a process though, is not the same as programming as a language. Don’t get hung up on mastering PHP first if you really want to develop in Ruby “on Rails,” for the web. You don’t have to master HTML to start learning scripting languages for web programming, but you do need to know enough to build a webpage using those languages. Learn enough of the current toolset to start being proficient, and advance to the next one. Once you’ve learned the basics of a couple languages, the rest is mostly a matter of syntax and available functions. It’s how you learn to think about the problem, and break it down, that’s important.

For the web

For web-based applications, the baseline is to learn HTML. No, its not a programming language, but it IS a structured language that defines what kind of information you’re putting on the page, and where. It is used in combination with CSS: stylesheets that allow you to separately define how that information looks once it is on the screen. Since any programming for the web will involve manipulating and modifying HTML and CSS, you need to know these. HTML is also a subset of XML - which is used as a document and information storage format by many programs.

The next layer to add once you’ve become comfortable with basic tags would likely be Javascript as it’s easily integrated into any web page and required to handle any interaction after the page is loaded. 

For more advanced programming - the next step is learning how to change and construct what HTML, Javascript, etc. web server delivers to the user dynamically. PHP is a common language that is easily integrated into web pages and allows you to do this, while MySQL is a database language so you can store and recover data for later use. From there, you can pick up Java, Perl, Python, Ruby, or even C for more advanced programming.

Mobile and Desktop

Unless you’re building low-level driver code, this will eventually require you to learn a set of daunting “API’s” that allow you to open windows and otherwise graphically manipulate data. For Macs and iPhones, you need to learn Objective-C. For Windows, check other people’s recommendations, but you’ll end up learning some variant of C, C++, or C# and the .net libraries. For Android based devices, and a number of other applications, you will need to learn Java. 

Again, the real trick is to learn to think like a programmer. All else is syntax, and understanding what will happen when you type out any given command. The choice of languages here determines what types of features are already wrapped up for you, which ones you have to program yourself, and how quickly and flexibly you can write down the concept you’re thinking. 

There are arguments for starting in Java. If you’re planning on making a living outside of the Mac or Windows world, you definitely need to learn it. That said, I personally prefer Python as a starting point. It comes pre-loaded on Macs, it’s easily available for Windows, it has a very clean syntax, and it easily bridges the gap from simpler, “procedural” languages to learning object oriented ones. While relatively slow performance wise, it has been repeatedly used by experienced programmers to prototype out software for later optimization, or even tackle projects they never would have dreamed of in high-performance languages like “C”.

Either way, there are plenty of good sources of information and starting tutorials. Once you get your feet wet, you can start progressing into more complex languages that require more attention to details like memory allocation, but give you better performance in the tradeoff, or start learning the basics of creating Mac, Windows, Android, or iPhone apps.