adding methods on the fly

Renzo Tomaselli renzo.tomaselli at tecnotp.it
Wed Jul 10 06:26:35 EDT 2002


Alex Martelli <aleax at aleax.it> wrote in message news:<3VAW8.59624$vm5.2174084 at news2.tin.it>...

> It keeps looking to me as if you're making a lot of implicit assumptions,
> and that many of them are wrong.  What makes you believe, for example,
> that just exec'ing "def foo(self): pass" does NOT leave you "a compiled
> form" of function foo (in the locals dictionary used for executing)?
> You can marshal such objects to bytestrings, too, for storing as BLOBs
> or whatever.

Yuup! this was the big missing point. Indeed I need to marshal all
functions (user-defined methods, actually) down to my OODBMS (and the
entire Python library too). Trapping/restricting import and all that
stuff I know about.
Now I know how to move from string methods to function objects, which
can be attached as methods to classes derived from my C++ wrappers.
I think I got a fairly complete picture now, so I will start to
simulate something and see what happens.

> > I still wonder why it's so hard (from a C/C++ perspective) to walk
> > from a "def foo(self): pass" string to a callable object to be added
> > to a class. 
> But it's not.  exec that with a locals dictionary, and in the dictionary
> at key 'foo' you find exactly a Python callable object (a function
> object, to be precise).  What IS "so hard" about this?  def is an
> executable statement, used to create function objects, so, if you
> start with a def statement and want a function object, you, of course,
> execute the statement.  What COULD possibly be easier or mote natural?

True but not obvious. I spent days in searching through this list on
Google (and on C++ SIG as well). There are many messages about
extending/embedding but none came to the above conclusions when the
target is focused on methods.

Thank you very much Alex, let me know if you plan to visit this area
in the near future.
           Renzo Tomaselli      
---------------------------------------------------------------------------
TecnoTP s.n.c. Special Information System Design
Maso Pelauchi I38050 Ronchi Valsugana,  Trento TN  ITALY
Tel. +39 0461 773164      Fax. +39 0461 771514
e-mail: renzo.tomaselli at tecnotp.it   
---------------------------------------------------------------------------



More information about the Python-list mailing list