Defining Python class methods in C

Alex Martelli aleax at aleax.it
Mon Mar 24 07:25:59 EST 2003


Bryan wrote:

> thank you both for your help.  i didn't know that classes and types
> were two different things.  i coded it as a type according to the
> example in "python in a nutshell" and it worked correctly the first
> time.  i wish i never saw the cookbook example... it would have saved

I'm looking at the Cookbook version now (as I edited it for the printed
version of the COokbook) and it doesn't seem that bad to me.  The "Problem"
statement clearly says the use of the recipe is for migrating to C classes
previously coded in Python under the constraint that the thingy MUST
remain a class to ensure no mods are needed in client code; the "Discussion"
clearly mentions that the usual approach is to instantiate PyTypeObject
instead, and also where exactly "the self argument passed to the C
function" comes from, why in most it's not very useful (and in what
special cases it might be and how), and also that "the class instance
is passed to the C functions as the first argument in the class tuple".

All in all it seems to me the Cookbook (printed version, also available
online in O'Reilly's "safari" service) does a decent job -- if you
disagree, I would appreciate it if you could post an errata to O'Reilly's
site so that David and I get a chance to fix it "officially".

I do have the impression that the _online_ cookbook may not be as
good -- that should be taken up with ActiveState, the site's owners
and maintainers -- I believe that fixes we wrote for the printed
version were not "back-ported" to the online version and I do not
know if there are any plans for that, or for a concerted effort to
get authors to fix their existing recipes as per enhancements made
to the printed version, etc.  Still, if you mean the online version
when you mention the Cookbook, you may indeed have a point -- I
think the printed version (or the online "safari" access to it)
might be a better reference point than the online version.

> me countless hours of agony.  classes have data and methods and types
> have data and methods.  in python i write classes all the time, so
> naturally i gravitated towards the class example in c.  i take it from
> john's posting, that classes are uses to be compatible with older
> versions of python.

I hope that point was made quite clear in my posts and mail too,
not to mention the "Problem" statement in the printed Cookbook.


Alex





More information about the Python-list mailing list