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

Tobias M. tm at tobix.eu
Thu Jan 10 12:36:15 CET 2013


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.

Hugo Arts wrote:
> What piece of your code requires this list? Perhaps we can redesign 
> that to be rid of it.
Actually in my code it's not a list but a dictionary. The class is part 
of a network server and handles incomming packets. It reads the header 
of the packet and determines the packet type. The dictionary has the 
packet type as key and the method to handle the packet of the 
corresponding type as value. So the dictionary is used to look up the 
correct method to process the packet.

With this I try to avoid a lot of "if...elif..." statements and make it 
easy to expand the network protocol by just adding a new handle method 
and put it in the dictionary.


More information about the Tutor mailing list