News for hackers. Stuff that matters.
Re:Say “no” to import side‐effects in Python by chrismorgan on Saturday April 05, 2014 @08:38AM (#7536857)
In Python, side-effects are simply not acceptable at all. One should instead put the code with the side-effect in a function and call it.

This is the approach taken by gevent, which allows you to replace the entire I/O stack. But importing it does not effect the change; you must explicitly call code to do that. This is done thus:

    from gevent.monkey import patch_all
    patch_all()
c.f. http://www.gevent.org/gevent.monkey.html
Re:Say “no” to import side‐effects in Python by Mithaldu on Saturday April 05, 2014 @08:52AM (#7536895)
In Perl the act of simply importing that patch_all function would be a side-effect, as it's implemented as code in pure perl that changes the caller's symbol table. I suspect it would be fair to call your import up there also a side-effect, but that it slips the mind since it's implemented in the python core. :)
Re:Say “no” to import side‐effects in Python by einhverfr on Saturday April 05, 2014 @09:20AM (#7536980)
Indeed. Also there are times (Moose for example) where you want to do more than import a function as written. For example Moo and Moose create a custom function "has" at import time that is them attached to the caller's namespace.

The reason for this is that you want to fully attach the function in the space as a native method and this requires some closures to make work sanely. In short you want:

   package foo;
   use Moose;
   has bar => (is => 'rw');
to behave identically as:

   package notfoo;
   use foo;
   foo::has(bar => (is=>'rw'));
If you rely on the caller the second example would add a bar accessor to the caller's namespace (notfoo) rather than to foo. In short you on't want to use "has" in the Moose namespace. You want to add a custom function to the importing namespace.

As I understand it you can't do this in Python not because it is a bad idea (it is a very good idea sometimes) but rather because of limitations in the language (no multiline lambdas, which you need in order to do useful stuff with the symbol table in this regard unless there is an equivalent in terms of defining a method the calling class inside a closure in the imported package). But again, my knowledge of Perl is much better than my knowledge of Python, so I could be wrong.

When you get into this sort of metaprogramming, custom symbol table manipulations are not only helpful but downright necessary. It is what allows you to add a sophisticated MOP when such is not included in core.

Re:Say “no” to import side‐effects in Python by marcosdumay on Saturday April 05, 2014 @10:17AM (#7537130)
Technically, you can do this with Python, but you'll have to use some dark magic to get the importing module namespace, in order to manipulate it. It'll not be pretty.

Instead, in normal Python it is the importing module that must decide who has access to its own namespace, and who must live within a separated one. It has a completely different set of costs and benefits. (I sometimes wonder if Guido used Perl as a counterexample when creating Python - its principles are almost completely opposite.)

Re:Say “no” to import side‐effects in Python by ta0967 on Saturday April 05, 2014 @09:35AM (#7537026)

    from __future__ import print_function
Re:Say “no” to import side‐effects in Python by makomk on Saturday April 05, 2014 @09:49AM (#7537058)
As I understand it, "from __future__ import ..." statements are actually a special type of statement that doesn't actually import a module at all - they just use similar syntax for compatibility reasons. There is an actual __future__ module, also for compatibility reasons, but importing it has no side-effects.
Re:Say “no” to import side‐effects in Python by chrismorgan on Saturday April 05, 2014 @09:54AM (#7537075)
Future statements do import something as well, when run, but their primary purpose is changing the behaviour of the compiler for the current file. They have no behaviour outside of that file, so that is not a publicly visible side-effect.
Re:Say “no” to import side‐effects in Python by ta0967 on Saturday April 05, 2014 @03:47PM (#7538194)
Whether they import a module or not is - IMO - irrelevant. I was responding to this claim: In Python, side-effects are simply not acceptable at all. One should instead put the code with the side-effect in a function and call it. `__future__` imports are obviously and purposefully side-effectful, there's no `print_function()`.
Re:Say “no” to import side‐effects in Python by einhverfr on Saturday April 05, 2014 @09:45AM (#7537054)
That's a side effect, right? Your module after the import is not the same as it was before, or am I missing something?
Re:Say “no” to import side‐effects in Python by ak217 on Saturday April 05, 2014 @12:55PM (#7537590)
Future imports are not imports at all (the fact that they use the same syntax as imports is misleading IMO). They are tags that instruct the interpreter to turn on certain features only when interpreting the given file. They must appear before any statements in the file, and they take effect before interpretation of the file starts. You could argue that this constitutes an import side effect, but that's neither here nor there, and not helpful.
Re:Say “no” to import side‐effects in Python by chrismorgan on Saturday April 05, 2014 @09:56AM (#7537080)
It only changes the behaviour for the current file, so it's not a side-effect.
Re:Say “no” to import side‐effects in Python by einhverfr on Saturday April 05, 2014 @12:20PM (#7537482)
That seems too narrow a view of side effect.

Which of the following are side effects?

1. Initializing a point of sale printer, checking for errors, and raising exceptions if, say, it is out of paper? Let's say this is a cash drawer driver and the cash drawer connects through the printer, and if the printer is out of paper, the drawer won't open properly (this happens btw). I would call this a side effect as well as a separation of concerns violation btw. However it is not likely to be a visible change to other modules.

2. Check for the presence of a binary and if found, cache the path to it, perhaps instantiating another object to do so? Definitely a side effect there, but not publicly visible.

3. Initializing an external library's environment (as happened in this case)? Done wrong it crashes the system but I suspect the segfault was not intended. Again it isnt clear to me you have a publically visible side effect intended.