Python 2.4: Why only assignments to None are forbiden?
Peter Hansen
peter at engcorp.com
Fri Nov 12 22:55:32 EST 2004
Josef Meile wrote:
> I think in general assignments to built-in types, functions, and
> variables should be also forbiden.
Very little in Python should be *forbidden*. Certainly not
something like this! Maybe make it hard to do inadvertently.
Maybe require it be an explicit call to a special function
or something. But forbid it? Please no!
One perfectly good reason to assign to builtins (and there are others,
I'm sure) is to provide a "mock" function to replace a standard
one, for use during automated testing. See, for example, the
reference to a "mock filesystem" that I posted a few days ago,
where you can insert your own "open" function in the builtin
namespace, providing a fake filesystem that intercepts all
file creation and reading and simulates the behaviour.
You can do this easily on a single module, or several, just
by inserting a global into the module(s) in question, but if
you want to do it through an application it becomes a real
burden (and a way to let bugs leak through) to have to do it
that way. A single assignment override to the standard open
is much more effective and safe.
Every time this issue comes up I feel compelled to point out
this perfectly useful and valid use of assigning to builtins...
I really hope the message gets through.
-Peter
More information about the Python-list
mailing list