The positive side effects of building stuff

The one piece of advice I wish I had been given when starting out would be that when learning about something get into the habit of using that knowledge as soon as possible. Set up an development environment so that you can build prototypes or small projects. Keep this environment up to date so that when you need to try something you can focus on the new thing and not exhaust your enthusiasm updating your IDE, programming language or Operating System.

In my experience benefits of trying stuff out adds that special sauce that will help you solidify what you have learnt and shine a light in areas you don’t. I was reminded of this recently when returning to Python after a gap of several years. Despite going through Pluralsight courses and skimming over books I discovered it is easier to remember the scoping rules of a Python program when working on actual program rather than trying to recall what I read in a book or watched in a course.

As an example of the benefits here are some of the things I have learnt whilst building a Hacker News webscraping tool.

Scope within __main__

Once I started refactoring the code away from __main__ I was surprised when code originally written as this:

if __name__ == "__main__":
    
    greeting = 'Hello world'
    
    print(greeting)

> Hello world

still continued to work when rewritten to:

def do_something():
    print(greeting)

if __name__ == "__main__":
    
    greeting = 'Hello world'

    do_something()

> Hello world

How was the greeting variable visible to the do_something() function? Fortunately this Stackoverflow answer explained that because greeting was declared in the __main__ module meant it was a global.

find_next_sibling and Lambda functions

Scraping information from Hacker News web site is straightforward with use of the Beautiful Soup find method. However this needed tweaking in order to get to the number of comments which were buried in a chunk of HTML.

Fortunately this challenge lead me to discovering two solutions. The first required chaining the Beautiful Soup find_next_sibling function and the other was the use of a Lambda function. Both of which I didn’t know existed and sent me off on a journey to find out more.

Automatic tuple unpacking

How do you return multiple values from a function? Should I use multiple variables or a tuple? The best answer I found was this one on Stackoverflow which explains that a tuple is created and unpacked automatically.

I verified this behavior by creating the following prototype in which the function foo returns two values.

def foo():
    x = 1
    y = 2

    return x, y

if __name__ == '__main__':
    a, b = foo()

Stepping through the code using a debugger I see the return type of the function is a tuple:

Google Style Python Docstrings

I don’t have time for comments such as

So for the docstrings I became familiar with the Google style of Python docstrings which is a pointer to official Google style guide.

Summary

I am still amazed how much value I get out of building a small coding project. It has proven to be the acid test when it comes to seeing how much I actually know compared to what I think I know.

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.