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”:

8753197-9976584-thumbnail

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:

8753197-9976599-thumbnail

 

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.

8753197-9976608-thumbnail

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

8753197-9976617-thumbnail

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 python.org 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

or

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.

Blender

Blender is a 3D modeling, animation, and rendering program with capabilities bordering on the professional (and certainly with features I only wish I had in my Lightwave 7/8 days) that is, nevertheless, absolutely free.

Yes, free. And it runs on a Mac, on Windows, and in Linux. 

Will you get a big career in 3D animation using Blender? Probably not, even if it was used for animatics in Spiderman2. What it will let you do, though, is learn the basics of building models, creating surfaces and textures, and animating your creations without spending thousands of dollars for Modo, Lightwave, 3DSMax, or Maya. I think the “free” price tag (compared to the thousands Lightwave cost when I bought it) and its inherent capabilities combines with the opportunity learn the thought process of 3D modeling more than make up for the learning curve of a new set of tools once you decide that you wish to pursue it further.

What You Don’t Do

Sometimes I can’t believe I first learned this concept from a Dilbert Cartoon. Even if desperate for work, you must realize what you don’t do. And learn to say “no.”

It’s not about being an ass. It’s about focus. It’s liberating, and counterintuitively, if delivered right, it can build trust.

It’s liberating because you don’t end up obligating yourself to commitments you’re not prepared to handle. Saying yes to things you can’t provide because of lack of tools or time, or because they are well outside of your experience places an immense burden upon you where you’re obligated to them. As long as you have the power to say “no” – your fate is in your hands, and you retain your own position of power in the negotiating process.

It builds trust because if you do make commitments you can’t keep because of conflicts with other commitments, or simply because it’s not your expertise, you end up setting yourself up for failure. Whereas saying “no” – if it comes from self-assurance, from the certain knowledge that here is your focus, and that what you’re being asked isn’t, that you can’t promise to deliver what they expect, you come across as mature, reliable, and a person who makes good judgements about the proper tool for the job.

If you think I’m kidding, we landed a long-term client because after looking the location over, we told them up front that we weren’t experts on everything they had on site, and weren’t sure they were a good fit for us. They decided they didn’t really need some of the other services, but really needed what we could provide.

When you’re in consulting, you trade on your reputation, and like life in general, on the perception of your reliability. Showing good judgment by saying “I’m not the man for the job, but I think he is” goes a long way to building and maintaining that reputation when it comes sincerely.

As an aside – there are always gray areas. We ended up picking up that client and learned a lot from it – but a huge part of why we’re both happy with the relationship is because we both understood what we could provide and what they expected from the beginning. At the same time, you shouldn’t stagnate – always push the boundaries a little bit to step outside of your comfort zone and learn something new. But never lose your focus. Know what you do, be prepared to tell people what you don’t do – and offer to help them find someone who’s a better fit. 

If they’re talking to you already, the odds are you’re close enough to what they are looking for that you can probably be a huge help for them – even if it’s pointing them to someone who really fits their needs.

Skitch

Skitch is my screenshot software of choice. Not only can you specify and take a snapshot, but you can then mark it up to hilight and point out relevant info, as well as draw mustaches. Then you can upload the results directly from Skitch or conveniently drag it to wherever you need it, be it a new email or your desktop.

Update: The company was bought out by Evernote. It’s now available for free from the Mac App store. 

DropBox

I can’t say enough good things about it. Dropbox provides transparent, constant, internet-based backup and synchronization of a folder that remembers previous versions, with a few extras thrown in to allow easy sharing. It works on the Mac, on Windows, on the iPhone, the Android, the web, almost anywhere you can get to the internet. And it starts out free.

But what does this mean?

It means that you get a folder on your computer that acts like any other folder. You can move or copy files in and out just like any other folder. And because it is a local folder, it’s just as fast as any other folder on your local hard drive. But this folder has the magical property of being backed up, to the internet, in the background, and that backup is updated every time you make a change. 

And that backup is tied to a user account and login. Add another computer to the account, and anything you move into the dropbox on one computer ends up on the other computer. Or it’s now visible via the dropbox app on your iPad or mobile phone.

Or in a pinch, at a friends house, you can log in via the web and download the files that way.

The fact that it does this transparently in the background is awesome enough, but that’s not all! Every time you make a change, it keeps the previous versions for the last 30 days – so you can go back and recover that file you deleted or overwrote while on the road.

And that’s still not all. You can share any folder in your dropbox with other dropbox users that you specify. Now, anything you move into that subfolder shows up on their computer.

But, you ask, what if they’re not on dropbox? Or if I don’t want to share a folder with them? Well, there’s also a “public” folder. Move or copy a file in there, and right click on it, and select “Copy Public Link”. Now send them the link, and they can download the file directly from Dropbox. No need to fiddle around with yousendit, etc. for those occasions something’s too big to email.

Dropbox starts free at 2GB of storage, and you can pay for more. You can also refer your friends to get extra free storage for everyone who signs up a new account using your referral link.

And joking aside, it is an outstanding product that they constantly improve to make it faster, smaller, and more reliable than it already is. I evangelize it every day, and I keep all of my “current project” files within my dropbox folder. If I lose my laptop away from home, I’ll be able to recover all my current stuff up to my last internet connection, and anything older is archived to other backups anyway.

Providing Reliable Backups for Friends, Family and Very Small Businesses

Tying in with my ode to Dropbox under the “What I Use” section, is this recent Lifehacker article that does an outstanding job of covering how to make reliable backups for the relatives most geeks provide tech support to, that are also transparent enough to pass the Aunt Hattie test. The article also points out how to set up fairly painless password storage (I also love 1Password for that), and remote access for giving them a hand. While it doesn’t cover much on local backups, modern Macs have the excellent Time Machine, and for Windows, the recommended Mozy will not only backup offsite, but will also back up to a local drive for faster recovery than the offsite backups. 

In short, this “for your relatives” solution is also perfect for the small, home-office or one one-or-two computer type of business for setting up reliable backups and remote support.

Coaches, Wisdom, Challenges, and Success.

Mike, a friend of mine told a story and asked a question. The story? A young man, hooked on playing football, goes through numerous failures. Over and over again he fails, is tackled, injured, hurt. He slams into the unforgiving earth, he bleeds, he is covered in mud and sweat, grime and spit. And the coach keeps pushing him – pushing him on. The kid keeps going on little more than faith. Faith that if he keeps trying, if he doesn’t stop running, doesn’t stop moving, no matter how hard he’s blocked, he can make it, he will make it. Because the coach said so.

And yet again, he fails – over and over. 

And he keeps trying, facing down the desire to quit, the pain, the suffering on, at times, little more than a bare thread of faith and dogged will. 

And yet. And yet.

There is a paradox. You have a gigantic heap of sand in your driveway. Your neighbors, your friends, your wife, your kids – they all stand about this massive, gigantic heap and proclaim that yea – it is a gigantic heap of sand and no mere pile. Pulling out but one speck, one grain of silica makes it no less a gigantic heap, and neither does pulling out the next, or the next, for at each point the neighbors, the kids, the friends, and all the gathered busybodies proclaim that lo, it is still a gigantic heap of sand and no mere pile.

No-one can draw the line at which removing but one tan, translucent grain of silica turns the gigantic heap into a pile. And yet, at some point, you look around, and realize that the once large gigantic heap now is but a mere pile. Wherever that line was, it was crossed a long time ago.

One day, invisibly crossing a faint line that cannot be seen, everything starts coming together. The boy starts to truly perform. He “keeps on choppin.’” – And he sees results. And then, he breaks through. 

He achieves his goal.

The question becomes: “Now What?”

What do you say to this young man? What do you say when he comes off the field? What is the paradox? What dilemmas does he now face? Why do others choose a different path instead?

I cheated, and added a bit. I also need to take a few side trips into wisdom, storytelling and sequels, but it’s all relevant. 

A caveat – I don’t know this boy, this man becoming. He could be anyone. All such advice is dependent on too many variables to predict. And yet – some truths re universal, or at least generally so. 

As we grow older we get cynical. It’s all too easy to remember that the good guy doesn’t always win, that everyone has their faults and imperfections. Those cynics, those that hate to think of the glory we are capable of, who tear heroes down via their failures in order to stand at the same level, are a poison. Being a hero isn’t about perfection, it’s about risking your life and facing danger in striving toward an ideal. It’s about standing fast on principle, and meeting a standard, and in some small way surpassing our humanity despite our other faults. Do we choose to look at our lives as a list of the things we’ve failed at, or a list of thing’s we’ve accomplished – with plenty of “interesting times” and bad examples not to emulate again?

Faith – faith that, despite the repeated failures, that the seeds of heroism lie in all of us if we but try. That we can at times approach the divine, and transcend our humanity in some small way. That in the wreckage, and blood and pain we find not despair but hope – and the will to go on.

This heroism, this struggle, this transcendence needs to be recognized and praised. It must be nurtured. And so we praise him. And this praise, coming from the man who’s standards he never quite met, who kept pushing him to try over and over again through the example that he could do it, will mean more than gold, more than wine, women, and song.

And it is this very struggle that shaped the man, that allowed him to make those small, incremental improvements, that molded him, toughened him, forged him, until he broke through and transcended his limitations. And he finds himself standing there, getting praise from his coach, knowing that he made it there, and never quite knowing when he took that step that made it.

The man now has a living, breathing example that he can face adversity and win. Maybe not always, certainly not always, but eventually, if he never lets go, he can win, or die trying.

And here lies the trap. 

The buddhists take the attitude that life is pain. A popular powerpoint presentation making the email rounds made the point that “assisting” a butterfly in its’ struggles to break free of the cocoon instead cripples it, as that very struggle is needed to develop its wings. And yet we want everything to be easy.

Despite the success and the clear path of how he got there, it is far too easy to rest on his laurels. He has succeeded. He has overcome! He won! Woo Hoo! He’s going to Disney World!

And if he’s like all too many stereotypical high school football stars, or like your standard-issue teen pop music/child actor celebrity, his life will implode.

He must immediately be presented with a new challenge. Any challenge, as long as it forces him out of his comfort zone and forces him to learn.

Something to focus on.

He’s gained some wisdom, and is perilously close to throwing it away. Invest a bit more wisdom in him that yet again, if he trusts you, and in this context he will now trust you more than ever, may save him until he learns from personal experience the why of what you are telling him to do. 

So go ahead. Take that break. Then have him get back on the damned horse, and ride! Push the envelope. Find another struggle where he can say “by God I accomplished something.”

But what struggle?

Here we turn to storytelling, and sequels. One trick to make things more bearable, that kids almost instinctively do, is to act like their heroes, to act as if they are the heroes in a story. Superman, the Lone Ranger, the super secret agent. (In this context, it begins to make you wonder about a lot of our sports heroes, celebs, and teen idols as role models, eh? It gets worse when you consider how many teen-oriented shows make the adults out to be idiots…..).

Part of why this works is because kids know the hero is supposed to struggle. If you are the protagonist, the hero in your very own story arc – unless your tastes run to the post-modern and nihilistic – you will instinctively start looking for solutions to the problems, and be more willing to face the problems.

The problem with sequels as a metaphor is Hollywood disease. Every sequel has to be bigger, better, louder, and with more explosions. Instead, I’ll turn to the authors who helm the podcast “Writing Excuses” (“Fifteen minutes, because you’re in a hurry, and they’re not that smart.”). All you need is a challenge. Any challenge, as long as it makes you focus, as long as it makes you learn something new, as long as it makes you grow.

So no, if you won the game, you don’t have to win the state finals or the superbowl (though those are worthy goals in and of themselves). Saving the world doesn’t require you to save the galaxy the next time out.

It can be as simple as finally getting your head wrapped around algebra, or grammar, while not slacking off on the football.

As one of my favorite bands put it:

There is no love untouched by hate
no unity without discord
there is no courage without fear
there is no peace without a war
there is no wisdom without regret
no admiration without scorn

(The Cruxshadows, “Eye of the Storm”, from DreamCypher)

It’s pointless to avoid struggle. Embrace it, face it, learn from it.

Addendum: Mike Bronco, the guy who originally asked the question that inspired this, is a all-around great guy and fitness instructor originally from Jersey. He’s got a book coming out called Man School, about the obvious, but sublimely so. It’s filled with real-life examples, good advice, and a ton of tales passed down from his Dad, Grandad, and uncles. 

TextWrangler and BBEdit (and TextMate too)

Ahhh, text editors. All of these are designed from the ground up to handle text, and specifically, write computer code and web pages, with syntax highlighting and color coding to make it easier to tell what’s going on.

TextWrangler is the free little brother to BBEdit and replaced BBEdit Lite. It is so powerful that it’s almost hard to justify paying for a full-blown editor until you are heavily involved in coding on a regular basis and really need some of the power-user features like code block folding, macros, and snippets that BBEdit and TextMate provide. File comparisons and graceful handling of large files also make the Bare Bones editors hands down winners compared to TextMate.

 I’ve used TextWrangler for years, but use BBEdit now. I own TextMate, really like it, and used it as my primary editor for a couple years, but it chokes on large files and hasn’t had any significant work done on it in a while to cover some of the odder quirks or oversights in an otherwise beautiful 1.0 release. Textmate 2 has been “coming someday” for quite some time now.

That said, TextMate is also much cheaper than BBEdit, and with TextWrangler to handle a prettier GUI-based file comparison when needed, can handle almost anything most people will throw at it. It’s more a matter of taste and style – much like the war between the also free and extremely powerful Vim and Emacs editors (also available in Linux) which I don’t use because at this point in my life, I don’t want to learn all the commands of yet another text editor until I have to. 

Play with all of the free ones, and even try the trials on the paid ones. Then settle in and really learn and get comfortable in one and stick with it as much as possible unless something truly new comes around. You’ll end up having to learn other editors and development environments (Xcode, Eclipse, etc.) often enough as it is, there’s no point in deliberately making your life more difficult.

Scrivener

My all-time favorite program for writing and research projects. It’s light on formatting and layout features, but doesn’t care. What it’s meant for is writing, writing, and more writing. And organizing that writing (as well as all the background info and research you accumilated, and characters, and…) in a clear, concise, effortless way. It also has export formats in place for screenplays.

Give it a shot. If you like the minimalist writing approach without the research and filing features, then also check out Writeroom.