New to Python: my impression v. Perl/Ruby

John J. Lee jjl at pobox.com
Mon Jan 19 08:21:15 EST 2004


Wayne Folta <wfolta at netmail.to> writes:
[...]
> So I decided this might be a nice exercise to try Python. And it went
> together very quickly using Python's POP3 package. Then I decided to
> try it in Ruby. I think one little portion of the code shows the
> difference between the two cultures.
> 
> The problem is that the message is not properly terminated, so you
> need to time out and catch that timeout to realize, "hmmm, malformed".
> In Python I had:
> 
> M = poplib.POP3 ('172.16.30.1') ;
> M.user ('foo') ;
> M.pass_ ('bar')
> 
> num_mesgs = len (M.list ()[1])
> bad_mesgs = 0
> 
> for msg in range (num_mesgs):
> 	try:
> 		M.retr (msg + 1)
> ... blah blah...
> 
> How do I get it to time out after 5 seconds and how do I catch that?
> The online docs are pretty good, but I had to guess that POP3 was
> built on the socket package. Looking at socket's docs I found the

Well, that's not exactly a huge leap given any knowledge of how the
internet works.  I'm not sure that Python's docs should be in the
business of educating people about that...


> proper command and exception. I had to include:
> 
> socket.setdefaulttimeout (5.0)
> 
> before the POP3 commands to set the default socket timeout to 5
> seconds. And
> 
> except socket.timeout:
> 
> is the proper way to catch the timeout. Both of these things are in
> the socket documentation.

...but the fact that there is no timeout support in client modules of
the socket module is a known bug:

http://www.python.org/peps/pep-0042.html
http://www.python.org/sf/723287

It's likely to be fixed in 2.4.  Timeouts on sockets were only
introduced in Python 2.3 (though there was a third party library
around for a long time before that that retrofitted timeouts into
the standard library).


> Contrast this with Ruby. The Ruby docs are less complete, but they did
> mention that POP was subclassed from protocol and you'd have to look
> at protocol's source to see how it works. Looking through protocol, I
> figured out what to do and it was more elegant.

Well, if Python's standard library had decided to subclass everything
from some 'protocol' base class, the docs would indeed mention that
fact, since that's would be a fact about the Python standard library
rather than about networking in general.  It didn't, so it doesn't.


> The protocol class had a read_timeout, but since Ruby's mantra might
> be said to be "Real OO", the POP code had been written such that you
[...]

In the absence of some explanation of what you mean by "Real OO", that
sounds trollish.

I don't see how a missing feature in Python reveals any difference in
culture between Python and Ruby, other than perhaps that the Ruby
people like implementation inheritance more than the people who
designed the Python standard library.


John



More information about the Python-list mailing list