Tuesday, January 27, 2015

Today's Python homework

The card I gave myself today says "Read Python language docs until I have written down 10 new things." Why? Somebody pointed out that var = val or '-' was cleaner than the mess I used instead, and I realized that much more effort is required. (Were it Perl, I'd probably have used or '-', but I can't always keep the features of the different languages straight.)

Here's today's list, which includes some things I'm familiar with in Python or other languages but can't pull out of a hat on demand.

  1. The file is UTF-8: # -*- coding: utf-8 -*- (or UTF-8 byte-order mark or VIM-style coding string)
  2. Identifiers named _* are not imported by from module import *. (I knew that a leading _ is for privacy but I didn't realize it was enforced anywhere besides analysis tools. But I don't think I use from module import * except with cascaded Django settings.)
  3. Identifiers named __* are class-private, and this is essentially enforced.
  4. Wow, combining string prefixes, as in ur or br, is new to me, as is using upper case string prefixes, such as R, bR, etc.
  5. I forgot about escape sequences for Bell, Backspace, Formfeed, and Vertical Tab, and never knew about \N{name-of-Unicode-character}. (I probably didn't know about octal escape sequences in Python either.)
  6. I don't always use whitespace to concatenate string literals, though I should where applicable.
  7. complex literals — 3.14j, 3.14e-10j
  8. ^ operator, for XOR
  9. the NotImplemented single-valued type, for returning from certain methods
  10. the Ellipses single-valued type, for indicating the presence of the ... syntax in a slice