On Sat, Aug 19, 2017 at 4:04 AM, Peter Otten <__pete...@web.de> wrote: > Steven D'Aprano wrote: > >> Mostly for Bob, but also for anyone else interested: >> >> When To Use Classes >> >> http://kentsjohnson.com/stories/00014.html > > Just a minor nit, but you don't even need a custom function for the callback > > result = [] > db.query(sql, result.append) > > The lesson is that Python already provides some powerful ready-to-use > classes that take you a long way without having to write your own custom > classes.
In my beginning experiments to date writing classes, I have often while writing my toy examples realized, "Why am I writing a class? Python already does this more straightforwardly with <insert Python featured code here>." Of course not knowing all of Python I sometimes don't realize this until well after I wrote the unneeded class. > Another alternative to (explicit) classes are generators which are an > elegant way to hold state and provide a simple interface. > > In modern Python db.query() should be an iterable to that the above can be > written > > result = list(db.query(sql)) I hope I don't forget this point between now and when I get the database part of my project going! > (That was easy; but I wonder what tkinter would look like without > callbacks...) I wish I knew more so that I could fully wonder about this myself. You might even be making a clever joke and I am clueless. >> - if you can't, encapsulate it in classes. > > I think it's important that you say "classes", not "class". As with > functions three small dedicated classes are much better than one big know- > it-all/do-it-all class. I try to keep this in mind. Another thing I'm currently struggling with is when to use inheritance vs. separate, independent classes. -- boB _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor