Generating methods in a class

C GIllespie csgcsg39 at hotmail.com
Tue Jul 6 08:12:36 EDT 2004


Dear All,

I have a class the looks something like this:

class file:
    def __init__(self):
        pass

    def file2ps(self):
         return 'ps'

    def file2jpg(self):
         return 'jpg'

    def file2gif(self):
         return 'gif'
    etc

What is a quick way of generating all these methods? I'm thinking along the
lines of:


class file:
    formats=['ps', 'ps2', 'hpgl', 'pcl', 'mif', 'pic', 'gd', 'gd2', 'gif',
'jpg']
    def __init__(self):
        for frmt in self.formats:
            self.__setattr__('file2'+frmt, ???)

I've tried a variety of things where ??? is, but with no success.

Any ideas?

Thanks

Colin





More information about the Python-list mailing list