[Tutor] Problem with calling class methods stored in a list

Dave Angel d at davea.name
Thu Jan 10 14:24:10 CET 2013


On 01/10/2013 06:36 AM, Tobias M. wrote:
> Peter Otten wrote:
>> Build the list outside the class: MyClass.method_list = [MyClass.bar] 
> Thanks, that is a solution. But I don't really like to put the list
> outside the class as it is strongly related to the class and not used
> outside.

But as it's the simplest solution, and one with no runtime overhead, you
really should consider it.  Having a line or two following the class
definition is not uncommon in Python, and comments can make sure the
reader of the code understands it's part of the class initialization.

If that really offends you, put the code in a _setup() method of the
same class, and run MyClass._setup() immediately after defining the
class.  _setup() can be a classmethod as well, and you could have it
protect itself against running more than once, perhaps by refusing to
run if the list/dict already contains entries.



-- 

DaveA



More information about the Tutor mailing list