On Fri, 2008-02-29 at 10:39 -0500, Ted Roche wrote:
> Eleven hearty souls braved the wind chills last night to attend the 
> monthly meeting of the New Hampshire Python Special Interest Group,
> held as usual on the fourth Thursday of the month at the Amoskeag
> Business Incubator in Manchester.

I talked about a "gotcha" where I tried to define __iter__ as a
classmethod.  When using SQLAlchemy it is often handy to define
classmethods which will act as constructors for the object instances
that reflect the database records.  These constructors often return
iterable query results.  I was planning to have __iter__ run a query
that would iterate through all of the records.

Unfortunately
        for record in Myclass:
                print record.name
                ...
does not work.  python will try to find an __iter__ method in the parent
metaclass and fail.  Implicit references to magic methodnames (__xxx__)
that *are classmethods* resolve to the parent metaclass.

Kent pointed out that
        for record in Myclass.__iter__():
will work and also provided a simple metaclass workaround.

On reflection, I think that the notion of making __iter__ a classmethod
is a mistake.  It preempts the possibility of an iterator for an
instance.  Providing a name (i.e. all) and coding
        for record in Myclass.all():
makes more sense.  I was getting carried away with cuteness.

-- 
Lloyd Kvam
Venix Corp
DLSLUG/GNHLUG library
http://www.librarything.com/catalog/dlslug
http://www.librarything.com/profile/dlslug
http://www.librarything.com/rsshtml/recent/dlslug

_______________________________________________
gnhlug-discuss mailing list
gnhlug-discuss@mail.gnhlug.org
http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss/

Reply via email to