[Python-Dev] METH_CLASS

Guido van Rossum guido@python.org
Thu, 25 Apr 2002 08:59:39 -0400

> The devel-docs tell me about METH_CLASS:
>   The method will be passed the type object as the first parameter
>   rather than an instance of the type. This is used to create class
>   methods, similar to what is created when using the classmethod()
>   built-in function. New in version 2.3.
> 1. This seems wrong. The first parameter the function receives
> is NULL, the type object goes together with the remaining arguments
> in the second parameter (which is a tuple).
> Is the documentation wrong, or is the code wrong (I didn't find
> any use of METH_CLASS in the CVS sources)?

I'll leave this for Fred to answer, since he implemented this.

> [I must say that I'm not really sure how this relates to the
> calling convention flags. Have tried METH_O and METH_VARARGS
> so far.]
> 2. Since this is marked new in 2.3: Is it planned to backport
> this stuff into the 2.2.x series?
> Requiring Python 2.3 for my stuff seems a but harsh currently...

I don't plan to backport this to 2.2 unless there's an overwhelming

> 3. While we're at it, although I could try to find out myself:
> How would one create a classmethod in Python 2.2?

You have to call classmethod() from C; it's available as
&PyClassMethod_Type, you can call it with e.g. PyObject_Call.  The
argument should be a function object that you created by calling
PyCFunction_New.  Then stick it into your type's tp_dict (after
calling PyType_Ready()).

I know that's complicated; that's why we added METH_CLASS. :-)
I'd recommend against trying this unless you absolutely need it.

--Guido van Rossum (home page: http://www.python.org/~guido/)