[Python-3000] Abilities / Interfaces

Jim Jewett jimjjewett at gmail.com
Mon Nov 27 20:01:55 CET 2006


On 11/22/06, Guido van Rossum <guido at python.org> wrote:
> On 11/22/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> > At 09:24 AM 11/22/2006 -0800, Bill Janssen wrote:
> > In Java, SMTP.sendmail would be something like this (using Python-like
> > syntax 'cause my Java is rusty):

> >      def sendmail(self, from, to_addrs:str, msg, ...):
> >          return self.sendmail(from_addr, [to_addrs], msg, ...)

> >      def sendmail(self, from, to_addrs:list[str], msg, ...):
> >          # main implementation

> Right. If this syntax was possible in Python lots of people would be
> very happy. But even the best generic function API I've seen is a lot
> more verbose than this -- there seems to be a separate set-up
> involved.

3rd-party registration is fundamental to extensible functions.  The
best you can do is to minimize it.


    def sendmail(self, from, to_addrs, msg, ...):
        raise TypeError(to_addrs must be a string or list)

    overload sendmail(self, from, to_addrs:str, msg, ...):
        return self.sendmail(from_addr, [to_addrs], msg, ...)

    overload sendmail(self, from, to_addrs:list[str], msg, ...):
        # main implementation

And then someone else could write

overload smtplib.SMTP.sendmail(self, from, to_addrs:Seq[my_str], msg, ...):
    to_addrs = [str(addr) for addr in to_addrs]
    return smtplib.SMTP.sendmail(self, from, to_addrs:list[str], msg, ...)


More information about the Python-3000 mailing list