[Tutor] Long post: Request comments on starting code and test code on chess rating project.

boB Stepp robertvstepp at gmail.com
Sun Aug 13 03:09:59 EDT 2017


It is rather late here, so I won't get to the links until much later
today, but ...

On Sun, Aug 13, 2017 at 1:42 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> I haven't had a chance to read the entire post in detail, but one thing
> which stands out:
>
> On Sun, Aug 13, 2017 at 12:22:52AM -0500, boB Stepp wrote:
>
>> I have started my coding with a RatingCalculator class.  The intent of
>> this class is to gather all methods together needed to validate and
>> calculate chess ratings.  My intent is to only ever have a single
>> instance of this class existing during a given running of the program.
>> (BTW, is there a technique to _guarantee_ that this is always true?)
>
> Yes, this is called the "Singleton" design pattern, which is very
> possibly the most popular and common design pattern in OOP.
>
> It is also very possibly the *least appropriate* use of OOP techniques
> imaginable, so much so that many people consider it to be an
> anti-pattern to be avoided:

[...]

> In Python, the equivalent to the singleton is the module. If you have a
> class full of methods which is only ever going to be instantiated once,
> *almost always* the solution in Python is to use a module full of
> functions.

Hmm.  I should have gone with my gut instinct.  I did not see any
value in a one-object class, and felt I should just have a module with
these functions.  But the goal of this project is to use OOP, so my
intended design was to *force* these methods into a class and use the
class as a convenient container.  OTOH, my other classes I plan to
code -- Player, Game and possibly Database -- make more sense as
classes as the first two will have multiple instances with different
states per object.  And eventually there will be a GUI in tkinter
which is naturally organized by classes.

Thanks, Steve.  This is especially useful feedback as it enhances my
understanding of when and how to implement classes vs. collections of
functions.

boB


More information about the Tutor mailing list