Am 10.01.2013 15:15, schrieb Peter Otten:
Of course handle_1_42() is not exactly the method name one would hope for.
You could, again, strive for simplicity and add a lookup table that maps
protocol tuples to function /names/ , but as simplicity doesn't seem to be
your cup of tea:
class HandlersType(type):
def __init__(cls, name, bases, classdict):
cls.lookup_protocol = lookup = {}
for k, v in classdict.items():
if isinstance(v, protocol_method):
lookup[v.protocol] = getattr(cls, k)
class protocol_method(classmethod):
pass
def protocol(x, y):
def attach_protocol(f):
f = protocol_method(f)
f.protocol = x, y
return f
return attach_protocol
class D:
__metaclass__ = HandlersType
@protocol(42, 17)
def foo(cls):
print "Hi"
D.lookup_protocol[42, 17]()
;)
In my eyes this is anything but simple.
Correct me if I'm wrong:
I already use a lookup table in my code, called "method_list" in my
first post. The advantage of your above code is that I don't need to
manually take care of the lookup table and extension by subclassing is
easier.
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor