adding attributes to builtin functions

Douglas Zongker dougz at cs.washington.edu
Mon Jun 24 22:04:45 EDT 2002


Peter Hansen <peter at engcorp.com> wrote:
: Douglas Zongker wrote:
:>
:>    <built-in function myfunction>
:>    Traceback (most recent call last):
:>      File "<stdin>", line 1, in ?
:>    TypeError: 'builtin_function_or_method' object has only read-only
:>      attributes (assign to .special)
:> 
:> Is there a way to define a builtin function whose attributes aren't
:> read-only?
:
: Maybe something like this?
:
:    [wrapper around the builtin]

Well, that works, but what I *really* wanted to do was to set the
attributes from within C.  I tried something like this:

    void initmymodule( void )
    {
        PyObject *m, *f;
    
        m = Py_InitModule( "mymodule", MyModuleMethods );

	// grab the function object
        f = PyObject_GetAttrString( m, "myfunction" );

	// set one of its attributes (to None)
        PyObject_SetAttrString( f, "special", Py_None );

	Py_DECREF( f );
    }

but it gives the same exception as doing it in Python ("builtin
function has read-only attributes").  I was hoping if someone could
tell me how to do it in Python I could translate that back into C...

thanks,
dz




More information about the Python-list mailing list