encapsulation, typing & other questions

Jason Orendorff jason at jorendorff.com
Fri Feb 8 21:04:42 EST 2002


> 1. Are there plans to include type support for method/function calls in
> Python?

No.

> Seems like it would lead to much faster error detection &
> correction.

It seems that way to me, too.  Many disagree.

In any case, this doesn't bother me as much in practice
as I expected it to, coming from C++ as I did.  Many things
work to offset this penalty:  Python's high-level-ness,
its wonderful standard library, its simple syntax, its
brevity, its readability, etc.

Sometimes error detection is quick but error correction is
a matter of "First, write an Adapter class..."  (Today I had
an ASP script fail because TextStream, a standard object
distributed by Microsoft, doesn't implement IStream, a
standard interface invented by Microsoft.  This is the
worst of both worlds:  the error isn't detected until
runtime *and* it's a static typing error that is
prohibitively difficult to fix.)

> 2. How easy is it to use design patterns in Python?

Extremely easy!  Here's a Singleton:

  class Logger:
      def __init__(self): ...
      def log(self, description, priority=3): ...
      def set_threshhold(self, threshold): ...

  logger = Logger()
  del Logger

Here's an implementation of Observer in pure Python:

  # An event object...
  class LineEvent:
      def __init__(self, target, line):
          self.target = target
          self.line = line

  # And an observable object that fires an event...
  class FileTailer:
      def __init__(self, file):
          self.__observers = []
          self.file = file
      def add_observer(self, obs):
          self.__observers.append(obs)
      ...
      def process(self):
          while self.has_more_lines():
              line = self.file.readline()
              for obs in self.__observers:
                  obs(LineEvent(self, line))

  # And an example of an object that observes a FileTailer.
  class FileWindow(JTextArea):
      def __init__(self, tailer):
          JTextArea.__init__(self)
          self.tailer = tailer
          tailer.add_observer(self.handle_line)
      def handle_line(self, event):
          self.text += event.line

(Jython also has special support for Java's event model,
which is not to be confused with what I'm illustrating above.
They're called "listeners" in Java, of course, and there are
other differences.)

The Distutils setup function is a Facade.

The standard library's SocketServer is rather like a
Template Method, in that it defines the common bits of
the algorithm and lets the subclass implement other bits.

Python 2.2 has new language support for iterators, so
the Iterator pattern is essentially built-in.

Python's __getattr__() provides truly remarkable support for
Proxy objects.  There are a few examples in the standard library:
Bastion (which is rather old) and weakref.proxy.  Mark Hammond's
win32com support creates Python proxies that look just like
COM objects, to the programmer.

If that's not enough to convince you, Bruce Eckel is writing
a book on the subject:  "Thinking in Python".  (It's on his
web site somewhere, but in very rough form.)

> A lack of typing suggests it wouldn't be too easy [...]

Maybe.  Or maybe static typing doesn't have anything to do with
design patterns.  Hmmmm.


> 3. Are there plans to include method & variable encapsulation in the
> object models in python?

No.

> The answer I've heard is that since it's possible to overcome
> encapsulation by long or dirty ways, it isn't worth it. But my
> comeback is that if one sticks to the rules while coding,
> this won't happen and the resulting gains are worth it.

You are right about the second part.

But the ordinary Python answer is *not* that encapsulation isn't
worth it, but rather that encapsulation by convention
is just as effective as encapsulation that's enforced by
the compiler.

Just "stick to the rules", as you say -- namely, don't call methods
named with leading underscores -- and you too can achieve
encapsulation in Python.

Your mileage may vary.


> 4. How advanced is db access in Python?

Not as good as JDBC, but then, what is?

It's a toss-up between Python's db support and Microsoft's ADO.
Python's API is probably a little better than ADO, but driver
quality seems to vary more.


> Does it use connection pooling
> and other optimizations like the JDBC drivers?

Are JDBC drivers doing connection pooling these days?

If I recall correctly, this is ordinarily done at a higher
level -- as it should be! -- by e.g. application servers.
Similarly, a Python application server would be expected
to provide connection pooling *on top of* the ordinary
database API.  Zope does this.

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list