[Tutor] dictionary of methods calling syntax

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Feb 8 19:58:56 CET 2012


On 08/02/2012 17:41, Gregory, Matthew wrote:
> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

This should help if you need more info 
http://code.activestate.com/lists/python-list/403361/

-- 
Cheers.

Mark Lawrence.



More information about the Tutor mailing list