suggestions for functional style (singleton pattern?)

Michael Torrie torriem at gmail.com
Sat Feb 28 21:19:32 EST 2015


On 02/28/2015 04:12 PM, yves at zioup.com wrote:
> For some scripts, I write in a a more functional way, using a lot of small
> functions outside of any class. Although it makes the code clearer for
> specific cases, I have found that it makes debugging and using the repl in
> general difficult, as as I have to re-initialise every single objects every time.
> 
> I have now started to use some kind of state pattern to alleviate this, here's
> a simplistic example:
> 
> https://github.com/dorfsmay/state_pattern_for_debugging_python/blob/master/dirstats.py
> 
> Are there better ways to address this? Any suggestion on this style?

You say you are trying to use a singleton pattern, but your code does
not appear to implement a singleton.  From what I can read of your code,
you really should just put all your functions as methods to the DirStat
class and call it good.  Especially if your module is going to be used
in several places potentially at the same time.

If what you want is a singleton, then that's what a module is already.
Module functions are singleton methods and module attributes maintain
state.  Just call your module dirstat and interface with it:

dirstat.opendir(**vars(args)
dirstat.print()

In this example, I really don't think the singleton is a good pattern,
though.



More information about the Python-list mailing list