ben sutcliffe

freelance web designer • berkshire uk

 

Learning Python


Last Summer I started learning Python, and it’s been a pretty steep learning curve – especially as I started learning working live on real code. As ever the learning is non-stop, especially as Python is the first language I’ve used off the web and on to the desktop. I’m going to post a few things that have (and on a regular basis still do) help me, just in case anybody goes through some of the same crises as me!

You can subscribe to the RSS feed available for these posts, or keep an eye out on my Twitter.

19
Apr

Tracebacks Rock

Whenever writing anything, pay attention to those tracebacks. Having only had the benefit of three-word PHP errors and next to useless JavaScript errors, I haven’t been used to checking out error messages. I’ve been getting better, especially with the WebKit developer tools in Safari and Chrome, but tracebacks in Python carry a lot if information.

You can view the outcome of a fatal error by running your application from IDLE, Python’s native editor. Once you’ve opened your app in IDLE, focus on to the shell window and execute the script (F5 on Windows). If you’re getting a fatal error but want to carry on executing after the bug is raised, use an exception to carry on past the error.

import traceback

try:
    # bad code here
except:
    traceback.print_exc()

Remember, tracebacks carry all that sexy information in a long list of jargon. But make sure you read the shell window, and never hide anything behind a try: in the hope that it’ll go away some day. I naively tried that a couple of times, but it just kept coming back to haunt me.

 
 
ben sutcliffe
loading content