implementing module functions==object methods in extension

Bernhard Herzog bh at intevation.de
Fri May 4 08:36:35 EDT 2001


"Steve  Holden" <sholden at holdenweb.com> writes:

> [Ferkles about in 1.5.2 library's string module]...
> 
> Yep. Certainly seems better than the previous implementation:
> 
> # Join words with spaces between them
> def join(words, sep = ' '):
>  """... """
>  return joinfields(words, sep)
> 
> # Join fields with optional separator
> def joinfields(words, sep = ' '):
>  """...
> 
>  (joinfields and join are synonymous)
> 
>  """
>  res = ''
>  for w in words:
>   res = res + (sep + w)
>  return res[len(sep):]
> 
> I especially don't like the way this builds a string with a separator at the
> beginning and then strips it off before returning, but I'm betting this was
> more efficient than other choices.

Note, that this is just the fallback implementation when the extension
module strop can't be found. See the end of string.py (from 1.5.2):

# Try importing optional built-in module "strop" -- if it exists,
# it redefines some string operations that are 100-1000 times faster.
# It also defines values for whitespace, lowercase and uppercase
# that match <ctype.h>'s definitions.

try:
	from strop import *
	letters = lowercase + uppercase
except ImportError:
	pass # Use the original, slow versions



  Bernhard
-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                               http://mapit.de/



More information about the Python-list mailing list