predicting function calls?

Roy Smith roy at panix.com
Tue Dec 27 23:53:18 EST 2005


I think I know the answer to this, but I'll ask it just in case
there's something I hadn't considered...

I'm working on a python interface to a OODB.  Communication with the
DB is over a TCP connection, using a model vaguely based on CORBA.
I'll be creating object handles in Python which are proxies for the
real objects in the database by doing something like:

handle = connection.getObjectHandle (className, instanceName)

Objects can have attributes (data) and operations associated with
them.  It would be very convenient to use the "." syntax to access
both of these, i.e. be able to say:

print handle.someAttribute
print handle.someOperation (arg1, arg2)

I'm using __getattr__() to process both of these constructs, and
herein lies the rub; I need to do different things depending on
whether the name is an attribute or an operation.  I can ask the DB
for a list of the names of all the operations supported by a given
object, but that's a fairly expensive thing to do, so I'd rather avoid
it if possible.  It would be really nice if I had some way to find
out, from inside __getattr__(), if the value I'm about to return will
get called as a function (i.e., the name is followed by an open
paren).  I can't see any way to do that, but maybe I'm missing
something?

One possibility would be to use different syntaxes for attributes and
operations, i.e:

print handle["someAttribute"]
print handle.someOperation (arg1, arg2)

but I really want to avoid having to do that for a variety of reasons,
not the least of which is syntax similarity with a legacy system.

The best I've come up with so far is doing the expensive "get a list
of operations for this class" call the first time I see an object of a
given class and caching the list.  The problem there is that this is a
very dynamic system.  It may be possible to add new operations on the
fly and I don't have any good way to invalidate the cache.

Any ideas?



More information about the Python-list mailing list