Try evaluating just
Module[
{g, x, expr},
expr = x^2;
g[x_] = expr]
You'll notice that this actually evaluates to something (it'll be something like x$41742^2
). Furthermore, the symbol g
inside the module is localized to that module (i.e. it's real name is something like g$12345
). So, inside the module you've defined DownValues for g$12345
, and in doing so, you made the whole Module evaluate to x$41742^2
. This you then set to the Global f
.
I know you didn't ask, but defining functions this way is fraught. The "this way" I'm referring to is by first creating an expression and then referring to that expression when creating DownValues. If you're going to do it this way, you might want to use Formal symbols (e.g. \[FormalA]
). Or there are other ways to protect your definition from collisions.