implementing module functions==object methods in extension

Steve Holden sholden at holdenweb.com
Fri May 4 08:01:35 EDT 2001


"Harald Kirsch" <kirschh at lionbioscience.com> wrote ...
>
> In the python-2.0 distribution I find that string.join() seems to be
> implemented as
>
> def string.join(words, sep = ' '):
>   return sep.join(words)
>
> I wonder if this is mostly historical or if it is still the preferred
> way to allow object methods to be called as module functions. I would
> rather have expected a pure C solution. Consider for example an object
> method with one integer parameter:
>
I'm guessing this was introduced for 2.0 (or maybe 1.6): since the string
method is coded in C it hardly seems worthwhile to duplicate that code for
the string module, right?

[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.

regards
 Steve





More information about the Python-list mailing list