__getattr__ and others

Grzegorz Dostatni grzegorz at ee.ualberta.ca
Tue Nov 12 18:32:05 EST 2002


Hi.

I want to create an object that (at runtime) is called with an arbitrary
function with arbitrary number of arguments.  Haveing this call it will
forward it through xml-rpc to the server, execute it and return the
results.

I am missing something simple - here is what I have:

    def calling(self, method="", *args, **kwargs):
        print "calling method ", method
        print "With arguments", args, kwargs

    def __getattr__(self, name):
        return lambda s=self, m=name, *args, **kwargs: apply(s.calling,
(m, ) + args, kwargs

This works as expected for (o is the instance of the object)

1. o.hello = OK. Returns the lambda function.
2. o.hello(keyword="Argument") = OK. that works.
3. o.hello(3, keyword="Argument") = This does not work.
4. o.hello(3) = This does not work again.

I have to do something like o.hello(o, "hello", 3) to get what I want.
Does anyone have an idea how to make it more consistent with normal
calling style?

Greg






More information about the Python-list mailing list