My CTO is learning Python....

Bjorn Pettersen BPettersen at NAREX.com
Sat Mar 1 05:30:55 EST 2003


...time to run screeming from the building <wink>. His problem, is that
"There doesn't seem to be any good books on Python". Pressed a little
more, it was refined to "either the documentation assumes you've never
programmed before, and spends volumes of text introducing loop
constructs etc." (that was the commentary on the Tutorial <sigh>), "or,
the author seems to be in love with the language purely for its advanced
and obscure features" (he'd hoped 'Thinking in Python' would be like the
C++/Java equivalent...). Pressed a little bit further, he commented:
"what I'm really looking for is a quick introduction for someone that
allready knows C++ or Java".

So on the positive side, our CTO is voluntarily learning Python -- on
the other hand, every time I show him the pydiomatic way of doing
something, his eyes get really big and he comments "you can do that? Why
isn't that in the documentation?" Followed by me pointing out where in
the docs it is, followed by "well, that's not very obvious..."

In case you were wondering, he's a self-admitted contrarian who expects
things to be the way he thinks they should be. He's also my boss (he
say, I do), and also probably one of the most brilliant analytical minds
I've ever met (i.e. I think he could do amazing things with Python if I
can manage to get him behind it instead of tripping over it :-)

Does anyone have a writeup or a reference to "pydiomatic problem
solving" especially for an audience of expert, large scale C++ and SQL
programmers? (I must admit I'm having problems getting people to think
of a Python solution, instead of first going 'how do I do this in C++',
and then 'how do I translate my C++ to Python?' -- advice graciously
accepted)

If I have to start from scratch, I would (so far) summarize from FAQ 6
(Python's design), 4 (Programming in Python), 8 (Python on Windows),
Selected sections from "Dive into Python", Python Cookbook?, DB API 2.0
(although I'm not sure I can justify $1200/devel for mxODBC yet...),
Intro to Numeric (we do numerical models), PIL/Piddle/PDFGen, Automating
Office from Win32 Programming, selected topics that have modules in the
stdlib (probably borrowing from effbot's work), Gordon's
Installer/Py2Exe, ??? Am I missing anything?

What I've been looking for, and not found, is examples of how dynamic
typing and interface polymorphism make your code shorter, simpler, and
easier. I will of course compare them to C++ templates (with the bonus
of seeing your code instead of all the type declarations), but I was
hoping for a couple of good examples. The one I have so far, is from one
of my projects (a "full" justifier for ascii text of various paragraph
types):

  # match space and markup before the actual text.
  class BulletItems:
      match = re.compile('r(\s*)([*\-+])(\s*)').match
      def __init__(self, match, lines):
          ...
  
  class NumericList:
      match = re.compile(r'(\s*)(\d+\s*[.\-]?)(\s*)').match
      def __init__(self, match, lines):
          ...
  
  class PlainText:
      match = re.compile(r'(\s*)').match  # always matches
      def __init__(self, match, lines):
          ...
  
  def getParagraph(paragraph):
      """Return an object of the appropriate Paragraph class, 
         depending on what text looks like.
      """
      # PlainText always checks as true, so we won't fall off the end
      lines = paragraph.split('\n')
      for klass in [BulletItems, NumericList, PlainText]:
          m = klass.match(line[0])
          if m:
              return klass(m, lines)

I'm looking for code that's similarly clear and straight forward in
Python and requires much more typing in C++...

-- bjorn

ps: I was sort of wondering why I didn't need to do "match =
staticmethod(match)" in the various paragraph classes? I know
re.compile(..).match is a bound method with self bound to the re object,
what I don't understand is why a bound method is special and can be used
as a staticmethod, while regular functions (def/lambda) cannot? Anyone?






More information about the Python-list mailing list