Sunday, January 31, 2010

Truth and Python 3: The bool Type

Python 3 has a bool type representing Boolean values. There are two builtin bool constants, True and False.

For legacy reasons, bool is actually a subtype of int and True behaves like 1 while False behaves like 0.

For example,

>>> True + 0
>>> False * 3

and even...

>>> True == 1
The main difference between True and 1 is that str(True) returns 'True' and not '1'. Similarly, str(False) returns 'False' and not '0'.

Any object can be converted to bool by running it through the bool constructor. Here are some examples:

>>> bool(True)
>>> bool(None)
>>> bool([])

For any object, x, if bool(x) is True, we say it's truth value is true and we consider x as true in a Boolean context. If bool(x) is False, we say it's truth value is false and we consider it as false in a Boolean context.

x's truth value is determined as follows:

  1. If x is None, it's false
  2. If x defines a __bool__ method that returns False or 0, it's false.
  3. If x doesn't define a __bool__ method but defines a __len__ method that returns 0, it's false.
  4. Otherwise, it's true

Given the rules above, it's no surprise that the vast majority of objects are considered true. The main ones that are considered false are:

  • None
  • False
  • The zero value of each numeric type i.e. 0, 0.0, 0+0j
  • Empty containers e.g. (), {} and [].
  • "" (the empty string)

Sunday, January 17, 2010

FireFox without a Mouse

I finished reading The Productive Programmer a few weeks ago and since then I've been thinking about how to be more productive on the 'micro' level.

"Keep you hands on the keyboard as much as possible" is one of the recommendations that people always make. Constantly moving your hand from keyboard to mouse and back slows you down and feels pretty awkward too.

With that in mind, I've been learning how to browse with FireFox using the keyboard only. Some sites make this difficult but I can now go for much quite a while without reaching for the mouse.

Here are some basic tasks and how to do them with the keyboard. Note that I've chosen the methods and shortcuts that I like best and that there are other ways of doing these things.

Entering an Address

  1. Use alt+d to focus the Location bar
  2. Type the URL
  3. Press enter to open the URL.
    You can hold these keys down to modify the behavior:
    • ctrl - prepend www. and append .com to the address
    • shift - prepend www. and append .net to the address
    • ctrl+shift - prepend www. and append .org to the address
    • alt - open in a new tab
    For example, pressing ctrl+shift+alt+enter with an address of python will open in a new tab.


  1. Use ctrl+k to focus the Search bar
  2. Type the search terms
  3. Press enter to open the search in the current tab
    You can hold down alt to open the search in a new tab instead

Navigating between tabs

On Ubuntu these shortcuts use alt instead of ctrl.

  • ctrl+tab displays the next tab
  • shift+ctrl+tab displays the previous tab
  • ctrl+n where n is the digit 1 to 8 displays the nth tab
  • ctrl+9 displays the last tab
  • ctrl+w closes the current tab

Searching within a page

  1. Use / to open the Quick Find bar
  2. Type the search text
  3. Use F3 to cycle forward and shift+F3 to cycle backward through the matches

'Clicking' on links

  1. Use ' to open the Quick Find bar for links only
  2. Type the search text
  3. Use F3 and shift+F3 to cycle to focus the link you desire
  4. Hit enter to 'click' the link. The following modifiers are available:
    • shift - open the page in a new window
    • ctrl - open the page in a new tab
    • alt - save the page instead of opening it