Inheritance but only partly?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Oct 3 23:56:52 EDT 2008


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



More information about the Python-list mailing list