How much sanity checking is required for function inputs?

Steven D'Aprano steve at pearwood.info
Sun Apr 24 01:49:36 EDT 2016


On Sun, 24 Apr 2016 12:34 pm, Michael Torrie wrote:

> There are many aspects to Pythonic programming, not just OOP.  For
> example using modules to store shared state for your program components
> is very pythonic, rather than using classes.  A module is kind of like a
> singleton instance, and still is object-oriented by the way (the module
> is an object).

I find myself going backwards and forwards on whether or not that is a good
idea. I think it depends on whether you are writing a library or an
application.

If you're writing an application, then storing state in module-level
variables works fine. Your application is, effectively, a singleton, and if
you try to run it twice, you'll (probably) be running it in two distinct
processes, so that's okay.

(If you're not, if somehow you perform some sort of trickery where you have
a single Python interpreter running your script twice in the same process,
then it will break horribly. But if you can do that, you're living on the
edge already and can probably deal with it.)

But if you're writing a library, then using module state is a Bad Idea. Your
library may be used by more than one client in the same process, and they
will conflict over each other's library-level state. "Global variables
considered harmful."



-- 
Steven




More information about the Python-list mailing list