What I Learned In Python This Week #6: Abstraction! (It’s Programmer for “Laziness”)

Tools I Used:

  • Geany
    • Light and functional GTK-based Python IDE.
  • Python shell / bash shell
    • I use the Python shell to learn functions interactively and the bash shell to test the scripts I write (though I use Geany’s “Execute” shortcut for quick testing).


  • Fibber (fibber.v1-0-0.py)
    • Calculates as many Fibonacci numbers as you tell it to. A demonstration of def, though probably could be written just as effectively without it.
  • Fiddled around with some older code for experimental/educational purposes.

Functions/Concepts I Learned:

  • Abstraction with def
    • def allows programmers to define their own functions. If a function you need isn’t built-in (or built the way you like), you can make one yourself. This lets you recycle your work without having to rewrite or maintain the same code in multiple places.
    • The most helpful aspect of def isn’t that it necessarily simplifies program structure (though it can and often does), but that it makes programs more human-comprehensible. This makes for more easily maintainable code (well, that and good comments), because you can focus on what a program does separately from how it does it.
    • A def function appears similar to a loop (e.g., def foo(bar): return bar + 1) and likewise is usually newlined then indented after the colon (apologies for an example which does not reflect this). You may nest loops within user-defined functions, and as of Python 2.2, you may nest user-defined functions as well.
    • To document your new functions, you can use regular comments, or you can use a docstring. You can access docstrings with the .__doc__ attribute (e.g., foo.__doc__ will return the docstring of the function foo()). In Python’s interactive shell, you can use help() in a similar way (e.g., help(foo) will give you the docstring of the function foo() and some additional information about it as well).
  • callable() lets you test whether something is callable, returning 1 if it is callable and 0 if it is not (remember that to Python these are Booleans).

I didn’t get to learn too many new concepts this week, due to the re-encroachment of my life upon my coding time. However, things should be looking up in the future — my wife just got two jobs and is in line for a third (they’re all part-time), which is a real blessing as she’s been looking for work since we were married in October 2009. This makes both our lives much easier, as my current job (though full-time) doesn’t pay enough to do anything (and I mean anything) beyond getting us to the next paycheck. Hooray for soon-to-come financial stability!

Methinks that next week and possibly the week after will be spent digesting chapter 6 as well as reviewing earlier chapters of Hetland. I was thinking that I could just run through Hetland multiple times until the deeper concepts soaked in, but my week-long hiatus really did a number to me mentally, and I need to go back and solidify the basics until I’m positive that I know them thoroughly. It’s not so much that I don’t know what Hetland is talking about at points, it’s more that I’ll try to go code something on my own that I know I ought to know, but can’t remember the exact functions/methods available to me. This is exactly like Greek and Hebrew class! Vocab is always my weak point — maybe I should make some flash cards, eh?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s