Re: Can I use the getattr functionality in a module?

Oliver Geisser (ogeisser@apollo.uni-paderborn.de)
24 Mar 1995 15:40:26 +0100

Guido.van.Rossum@cwi.nl wrote:
: > While working on my python khoros interface a question came to me. I
: > know how to use the getattr method while building a type within "C".
: > I was wondering if I could do the same but from within the module
: > interface itself. I would like to bind methods to my module
: > on-the-fly. For example:
: >
: > If some one trys this:
: >
: > import khoros
: >
: > a = khoros.get("lenna.xv")
: >
: > q = khoros.kflip(a)
: >
: > I would like to be able to trap the call to kflip in my "C" code and
: > query the khoros system to see if kflip is a valid method. This way I
: > can interface to all of khoros and only bring into-the-interface often
: > used methods, leaving seldom used methods to be dynamically bound.

: You can't do that for ordinary modules, since their getattr() is fixed
: and requires that the name is already defined in the dictionary.
: However you can do it with object types you define yourself (and I
: seem to understand that you are already doing that). I see two ways
: of exploiting this for the khoros module:

: - Instead of making these functions part of the module, add a magic
: object to the module and make them methods of that object -- it can
: then do the lazy import. Then tell your users to use methods of this
: object.

: - In the initialization of the module, manipulate sys.modules so that
: sys.modules['khoros'] is not a module but a special object as in the
: previous item (there may be a line in add_module that prevents this
: from working -- I'm willing to take it out).

Can you explain this a little bit further?

I'm asking because I'm thinking about an Obj-C Interface for Python.

The first simple implementation would do something like this:

import ObjC

cls = ObjC.get_class("SomeObjC_Class")
obj = ObjC.send_method(cls,"alloc")
ObjC.send_method(obj,"init")
ObjC.send_method(obj,"doSpam")

If I understand it right I could implement a better version:

import ObjC

cls = ObjC_Class("SomeObjC_Class")
obj = cls.send_method(cls,"alloc")
obj.send_method(obj,"init")
obj.send_method(obj,"doSpam")

But I want to do the following:

import ObjC

vw = View()
vw.initWithFrame(x,y,heigth,width)

ls = List()
ls.init()
ls.insert(vw);

Where View and List are ObjC classes from a library you have to link.
The Obj-C runtime system makes the following information available:
A lisf of classes; the class and instance methods and their argument/return
types. So everything is there. But what to do ?

-- 

Ciao, Olli

| Oliver Geisser * Steindruefft 11 * 33184 Altenbeken * Germany | | e-mail: ogeisser@uni-paderborn.de * Phone: 05255/7966 | | I prefer ASCII-Mail, but you can also send me NeXT-Mail |