469,949 Members | 2,195 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,949 developers. It's quick & easy.

Generating methods in a class

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
Jul 18 '05 #1
4 2001
C GIllespie wrote:
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:
class myFile: .... 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.
Jul 18 '05 #2
On Tue, 6 Jul 2004, C GIllespie wrote:
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.

Jul 18 '05 #3
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.
Jul 18 '05 #4
Thanks for all you help and suggestions

Colin
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

99 posts views Thread by David MacQuigg | last post: by
7 posts views Thread by Conor Maguire | last post: by
1 post views Thread by Adam Clauss | last post: by
3 posts views Thread by Raed Sawalha | last post: by
4 posts views Thread by Daniel Nogradi | last post: by
9 posts views Thread by Cesar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.