On Thu, Jan 10, 2013 at 7:48 AM, Peter Otten <__pete...@web.de> wrote:
>
> If you adopt this approach you might omit the lookup dictionary and use
> getattr():
>
>>>> class B(object):
> ...     @classmethod
> ...     def handle_foo(cls): print "Hello from B.handle_foo()"
> ...     @classmethod
> ...     def get_handler(cls, packet_type):
> ...             return getattr(cls, "handle_" + packet_type)
> ...
>>>> B.get_handler("foo")()
> Hello from B.handle_foo()
>
> (I've added the "handle_" prefix so an attacker can't just invent package
> types to have arbitrary methods invoked.)

This approach also makes it simpler to override handlers in a
subclass. You don't have to worry about separately updating a copy of
a list or dict.
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to