[Tutor] dictionary of methods calling syntax

Alan Gauld alan.gauld at btinternet.com
Wed Feb 8 00:51:30 CET 2012


On 07/02/12 19:32, Gregory, Matthew wrote:

> class Statistics(object):
>      STAT = {
>          'MEAN': get_mean,
>          'SUM': get_sum,
>      }
...
>
> if __name__ == '__main__':
>      spam = Statistics(4, 3)
>      print spam.get_stat('mean')
>      print spam.get_stat('sum')


Since a class is effectively a disguised dictionary I'm not sure why you 
want to do this? If you just want to access the method by name then why 
not just call getattr(spam,'get_mean')?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list