Inheritance but only partly?

George Sakkis george.sakkis at gmail.com
Sun Oct 5 23:01:29 CEST 2008


On Oct 3, 11:56 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Fri, 03 Oct 2008 03:32:52 -0700, Michele Simionato wrote:
> > IMO, if you have methods that you want to use in different classes, this
> > is hint that
> > you are in need of generic functions. See this blog post for an example:
>
> >http://www.artima.com/weblogs/viewpost.jsp?thread=237764
>
> That's a very interesting article, but I'm afraid I don't understand what
> makes them "generic functions" as opposed to just functions. Your simple
> generic example:
>
> from pkgutil import simplegeneric
>
> @simplegeneric
> def print_out(self, text, *args):
>     if args:
>         text = text % args
>     print >> self.stdout, text
> # and similar for print_err and readln_in
>
> class FileOut(object):
>     def __init__(self):
>         self.stdout = file('out.txt', 'w')
>
> print_out(FileOut(), 'writing on file') # prints a line on out.txt
>
> doesn't seem to do anything extra that the following would do:
>
> def print_out2(obj, text, *args):
>     if args:
>         text = text % args
>     print >> obj.stdout, text
>
> class FileOut2(object):
>     def __init__(self):
>         self.stdout = file('out2.txt', 'w')
>
> print_out(FileOut2(), 'writing on file')
>
> What's the difference?
>
> --
> Steven

Did you read the next section, "extending generic functions" ?

George



More information about the Python-list mailing list