Wrapping method calls with metaclasses
Lawrence Oluyede
raims at dot.com
Wed Dec 7 05:02:39 EST 2005
Il 2005-12-07, Alex Martelli <aleax at mail.comcast.net> ha scritto:
> I can't reproduce the infinite recursion you observe, by merging said
> recipe and your definition of class Test, w/Python 2.4.2 on Mac OS 10.4.
It seemed that the problem arose because I was not using super() in a new
style class based hierarchy.
class Test(object):
__metaclass__ = LogTheMethods
logMatch = '.*'
def __init__(self, foo):
self.foo = foo
def test(self):
return "test"
class TestChild(Test):
def __init__(self):
# with super this works
Test.__init__(self, "foo")
def test(self):
return "child"
d = {'test': Test,
'child': TestChild}
class MainTest(object):
__metaclass__ = LogTheMethods
logMatch = '.*'
def create_test(self, key):
return d[key]()
if __name__ == '__main__':
l = MainTest()
print l.create_test("child").test()
look in TestChild's __init__(). Not using super() fails with a
"""
File "/home/rhymes/downloads/simple_logger.py", line 55, in __init__
Test.__init__(self, "foo")
File "/home/rhymes/downloads/simple_logger.py", line 24, in _method
returnval = getattr(self,'_H_%s' % methodname)(*argl,**argd)
TypeError: __init__() takes exactly 1 argument (2 given)
"""
In my project I'm doing deeply nested recursion things and it exceeds the
maximum recursion limit for this problem this way.
I don't get to fully work the metaclass anyway for other weird reasons
(using super() I lose an attribute around?!) I'm gonna fix this.
--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
More information about the Python-list
mailing list