[Tutor] dictionary of methods calling syntax

Gregory, Matthew matt.gregory at oregonstate.edu
Wed Feb 8 18:41:26 CET 2012


Alan Gauld wrote:
> 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')?

Thanks for the feedback and, yes, this makes sense.  My use case was when the statistic desired was going to be specified at runtime (through file input or UI) and that a dictionary would be a convenient crosswalk to associate the statistic name with the method name (and thus avoid an if/else ladder).  But I understand that as long as there is a direct relationship between the name of the statistic and my class method (e.g. 'mean' -> get_mean), that I should be able to use the getattr() syntax as above.

Thanks also to Joel for the suggestion to put the dictionary inside of __init__.

thanks, matt


More information about the Tutor mailing list