Can __iter__ be used as a classmethod?

Michael Hudson mwh at python.net
Thu Mar 13 07:46:21 EST 2003


"Greg Ewing (using news.cis.dfn.de)" <me at privacy.net> writes:

> I don't find them horrible, I was just exploring whether
> it was redundant or nearly so, given the metaclass mechanism.
> It seemed as though the only extra thing it gave you was
> the ability to call class methods through an instance,
> which didn't seem startlingly useful to me.

I've remembered why I did this, now.  I have a bunch of classes that
describe records, which is to say bunches of fields and their
associated descriptions (string, integer, that kind of thing).  The
superclass has a class method "get_field_description" which is
usefully called both on instances and class objects:

if instance.get_field_description(f).acceptable(datum):
    setattr(instance, f, datum)

s = 0
for f in fieldnames:
    s += class_obj.get_field_description(f).datasize

(well, not quite like that because I'm trying to limit the length of
the post, but I hope you get the idea).

I probably wouldn't have wished for this ability were it not there,
but it's kind of nice to have it.

Cheers,
M.

-- 
   This proposal, if accepted, will probably mean a heck of a lot of
   work for somebody.  But since I don't want it accepted, I don't
   care.                                   -- Laura Creighton, PEP 666




More information about the Python-list mailing list