adding methods on the fly
Renzo Tomaselli
renzo.tomaselli at tecnotp.it
Mon Jul 8 11:02:19 EDT 2002
Alex Martelli <aleax at aleax.it> wrote in message news:<hikV8.47155$vm5.1686976 at news2.tin.it>...
> Renzo Tomaselli wrote:
> > 1. Can we add new (Python) methods to a C object wrapper, e.g. after it
> > has been instantiated with a normal (C) method table ?
>
> What happens when you try to add attributes to a C-coded object depends
> on how that C-coded object has defined its setattro slot (or its setattr
> slot, but that's an older and less general way to do things). And
> similarly for the getattro that implicitly happens when the attribute
> is "gotten" prior to calling it.
>
> So, it all depends on how you code your C extension.
Ok, I will investigate further about slots.
> > 2. if not: can we add a new method to an already imported Python class ?
> > If yes, how can we do it, assuming to start from a method statement hosted
> > by a string ?
>
> Given a Python class object, whether classic or new-style, setattr works
> just fine. Not sure what is "a method statement hosted by a string".
I mean a function declaration in a string. In short, what I miss is a
way to transform it into a callable object to be added to a class.
Whether such class is just a wrapper on my C++ object or a Python
derived class depends on issue below.
I'm lost here between PyRun_String and PyCompile_String, which seem
the only places where I can start from code strings to add methods on
the fly.
> > I understand that the latest version (2.2.1) allows for defining a class
> > which inherits from a C type, so that we can define some Python methods on
> > it, while retaining the capability of inherited C methods.
>
> Yes, as long as that C-coded type IS specifically coded as to support
> being inherited from. Once again, it all depends on how you code at
> the C level.
Could you please explain this topic a little further ? How do we
enable a C type to be inherited from by a Python class ?
> One way to avoid depending on how the C-level code is written is to
> use wrapping and automatic delegation in your Python class -- that
> also works in 2.1 and earlier, not quite as fast as inheritance but
> the difference may not be dramatic. For an example, see
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52295
> (or, better, the expanded and clarified version in the printed
> Python Cookbook, ed. Martelli and Ascher, but that's only due out
> in 3 weeks, so, meanwhile, the original online version will do:-).
>
>
> Alex
Ok, got it. The overall issue is dealt with in the opposite way I'm
interested in: almost all involved discussions on this list handle
about estending Python by C/C++ while I need to extend C++ by Python.
And possibly by adding just functions (methods), no extra class stuff
or modules.
Thanks for any suggestion,
Renzo Tomaselli
---------------------------------------------------------------------------
TecnoTP s.n.c. Special Information System Design
Maso Pelauchi I38050 Ronchi Valsugana, Trento TN ITALY
e-mail: renzo.tomaselli at tecnotp.it
---------------------------------------------------------------------------
More information about the Python-list
mailing list