Generating methods in a class

C

C GIllespie

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
 
R

Remy Blank

C said:
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?

What about this:
.... formats=['ps', 'ps2', 'pcl']
.... for frmt in formats:
.... exec "def file2%s(self): return '%s'" % (frmt, frmt)
.... del frmt
....
>>> dir(myFile) ['__doc__', '__module__', 'file2pcl', 'file2ps', 'file2ps2', 'formats']
>>> f = myFile()
>>> f.file2pcl() 'pcl'
>>>

HTH,
-- Remy


Remove underscore and anti-spam suffix in reply address for a timely
response.
 
C

Christopher T King

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.

You've probably tried using ??? = lambda: frmt, with the effect that all
the functions return 'jpg'. This has bit me before, too. The problem with
this approach is that frmt is not evaluated inside the lambda -- after the
loop has finished, you end up with a bunch of "lambda: frmt"s, and frmt is
left equal to "jpg". The way to fix this is to force frmt to be evaluated,
as follows:

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

def genfunc(self,frmt):
return lambda: frmt

Calling genfunc() forces frmt to be evaluated before being re-bound and
passed to the lambda.

There are a couple of problems with placing this all in __init__, however:
the functions are generated each time you create a new file object, and
they aren't passed a "self" reference. To fix these problems, it's best to
add all the methods to the class itself, and not the individual objects:

class file:
formats=['ps', 'ps2', 'hpgl', 'pcl', 'mif', 'pic', 'gd', 'gd2', 'gif',
'jpg']

def genfunc(frmt):
return lambda self: frmt
genfunc=staticmethod(genfunc)

for frmt in file.formats:
setattr(file, 'file2'+frmt, file.genfunc(frmt))

Note that neither formats nor genfunc() need to be members of file; if it
suits your taste better, the same thing can be written as:

class file:
pass

formats=['ps', 'ps2', 'hpgl', 'pcl', 'mif', 'pic', 'gd', 'gd2', 'gif',
'jpg']

def genfunc(frmt):
return lambda self: frmt

for frmt in formats:
setattr(file, 'file2'+frmt, genfunc(frmt))

Personally, I prefer the version using static attributes of the class, but
either way will work as well.
 
A

anton muhin

C said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top