Side-effects [was Re: I am out of trial and error again Lists]

Chris Angelico rosuav at
Sun Oct 26 07:42:06 CET 2014

On Sun, Oct 26, 2014 at 5:12 PM, Steven D'Aprano
<steve+comp.lang.python at> wrote:
> However, mutator methods on a class don't change global state, they change
> the state of an instance. Even random.random and friends don't change
> global state, they change a (hidden) instance, and you can create your own
> instances when needed. That gives you the convenience of pseudo-global
> state while still allowing the flexibility of having separate instances.
> Even global variables in Python are global to the module, not global to the
> entire application.

True, but they do still change state; I oversimplified a bit, but
certainly there are plenty of functions that change state in some way,
and are not frowned upon.

Module level variables really are global, though. You can't easily and
conveniently reinstantiate a module in Python; if you "import random;
random.seed(1234)", you can confidently expect that some other module
that uses random.random will be affected. Sure, there are ways around
that, but that's true of any form of global state - for a start, it's
usually "process level" state, in that spawning a new process will
isolate one from another. The only thing that isn't truly global is
the namespace; I can write "x = {}" and you can write "x = []" and
they don't collide. They're still global (process-level), it's just
that they're "foo.x" and "bar.x" and are thus distinct.

> I would say that Python is a pragmatic
> language which uses whatever idiom seems best at the time, but over all it
> prefers mutable state for compound objects, prefers immutable state for
> scalar objects (like numbers), and discourages the unnecessary use of
> global mutable state.

It's not really compound vs scalar, but yes, I agree. Practicality
beats purity, on so many levels. Functions with side effects aren't
considered some sort of weird beast that we have to permit for the
sake of I/O; they're a fundamental part of the language.


More information about the Python-list mailing list