Generating methods in a class

anton muhin antonmuhin at rambler.ru
Wed Jul 7 09:10:53 CEST 2004


C GIllespie wrote:
> 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
> 

Actually, I don't think it's a good idea---I'd rather prefer something like:

   class file:
     <skipped>
     def convert(self, type):
        return type

with further dispatch on type if needed or just a bunch of functions put 
into a dictionary.

If you insist on methods you can take a look on __getattr__ machinery.

And the last: `file` seems to be a bad name as it hides standard `file` 
class.

regards,
anton.



More information about the Python-list mailing list