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

Tobias M. tm at tobix.eu
Thu Jan 10 14:49:11 CET 2013


Dave Angel wrote:
> 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.
>
Interesting objection! However I'll probably implement the solution 
using __get__(None, cls) to get the method from the dictionary. It 
doesn't really add complexity and the additional overhead is 
insignificant in my use case. There will be only one client connected at 
a time and not much communication. The time the server will need to 
process the packet data will be disproportionally larger than the time 
to read the header and choose the handler method.



More information about the Tutor mailing list