how to dynamically create class methods ?
steve at REMOVE-THIS-cybersource.com.au
Thu Mar 27 09:12:03 CET 2008
On Wed, 26 Mar 2008 00:27:17 -0700, Arnaud Delobelle wrote:
>> I'm hardly surprised. The naivety of those who insist that the "best
>> way to understand how new.function and new.code work" is to look at the
>> C source code for object is amusing.
> Since 'those who insist...' are just one person, me, you might as well
> name them. Firstly I didn't 'insist', I stated it once. Secondly I'm
> glad that I provided you with a few chuckles, but to proclaim that one
> is 'amused by the naivety of others' doesn't make one automatically
Your criticism is accepted.
You're not the only person who says "Read the C source", so I wasn't
pointing the finger at just you.
> Do you know how these functions behave? Have you looked at their
> documentation? new.function and new.code (implemented as Objects/
> funcobject.c:func_new and Objects/codeobject.c:new_code) are not really
> documented (new.code.__doc__ is 'Create a code object. Not for the
> faint of heart.'). If you can't read the source then I don't think you
> should use the functions.
I don't know about that. People reverse-engineer programs without access
to the source code all the time. Reading the source code may (but not
always) make understanding the functions easier, but it's not compulsory.
If I cared enough, the way I'd go about reverse-engineering the functions
would be to start by pulling apart a Python function into a code object,
then pulling apart the code object into its components, making small
modifications to the components, then reversing the process (using
new.code() and new.function()) to build up a function object I could call.
Or I'd ask somebody who already knew how they worked to give an example
or two of Python code that called new.code() and new.function().
It's not ideal, of course, in a perfect world whoever wrote new.code()
would have written some better documentation, but it will do for non-
For production code that I relied on, I'd hire a C programmer to read the
source and document it for me.
More information about the Python-list