predicting function calls?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Dec 28 07:00:57 EST 2005


On Tue, 27 Dec 2005 23:53:18 -0500, Roy Smith wrote:

> 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.

[snip]

> 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).

How do you decide whether handle.foo should be treated as an attribute or
an operation?

If your object has an attribute foo, then what should you do when somebody
calls handle.foo()? That is, they treat an attribute as if it were an
operation? And vice versa.

In other words, it is not sufficient to know whether handle.foo is being
used as a operation or an attribute, but you need to know whether it
*should* be called as an operation or an attribute.


-- 
Steven.





More information about the Python-list mailing list