Metaclass with name overloading.
len-1 at telus.net
Tue Sep 28 05:07:06 CEST 2004
Carlos Ribeiro <carribeiro at gmail.com> writes:
> On Mon, 27 Sep 2004 21:23:14 GMT, Lenard Lindstrom <len-1 at telus.net> wrote:
> > Carlos Ribeiro <carribeiro at gmail.com> writes:
> > > <sample code snip>
> > > The problem is that the methods were not bound to the instance. Adding
> > > individual names to each method won't work, because it'll not bind the
> > > references stored in the overload_list. I thought about using a
> > > closure or curry type of solution, but that's something that I still
> > > don't understand very well. Any tips?
> > >
> > Here is my take on decorator overloaded. I implement OverloadedFunction
> > as a descriptor. It supports method binding.
> That's what I was missing. I've read about descriptors last week, but
> didn't had the time to get a hand at it. It's interesting. My
> development machine is still using 2.3 -- I don't know if this
> descriptor fancy stuff would work here... *btw, that's why my original
> snippet didn't use the new syntax to call the decorator).
Descriptors were introduced in 2.2. This version works with 2.2 and
up. The previous example only worked with the new decorator syntax.
def __init__(self, functions, instance, owner):
self.bm_functions = functions
self.bm_instance = instance
self.bm_owner = owner
def __getitem__(self, index):
def __init__(self, functions):
self.of_functions = functions
def __get__(self, instance, owner):
listattr = '_%s_functions_' % func.__name__
attrs = sys._getframe(1).f_locals
functions = attrs[listattr]
functions = [func]
attrs[listattr] = functions
# Test case:
def __init__(self, member):
self.member = member
return "f 0: member=%s" % self.member
f = overloaded(f)
def f(self, s):
return "f 1: member=%s, s=%s" % (self.member, s)
f = overloaded(f)
print b.f("Yet another f")
> I think that this code is now Cookbook-ready. Any comments?
Unfortunately it does not work as is with the staticmethod and
classmethod wrappers since these do not define either a
__name__ or fn_name attribute. And it takes an extra bit of
convolution to get at the functions wrappered by these objects.
There are more callables in Python than your philosophy can
imagine. And each requires its own kind of introspection.
<len-l at telus.net>
More information about the Python-list