<p><br>
On Feb 5, 2012 5:36 PM, &quot;Steven D&amp;apos;Aprano&quot; &lt;<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>&gt; wrote:<br>
&gt;<br>
&gt; Paul Moore wrote:<br>
&gt;&gt;<br>
&gt;&gt; On 4 February 2012 11:25, Steven D&#39;Aprano &lt;<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>&gt; wrote:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; It strikes me that it would be helpful sometimes to programmatically<br>
&gt;&gt;&gt; recognise &quot;preview&quot; modules in the std lib. Could we have a recommendation<br>
&gt;&gt;&gt; in PEP 8 that such modules should have a global variable called PREVIEW, and<br>
&gt;&gt;&gt; non-preview modules should not, so that the recommended way of telling them<br>
&gt;&gt;&gt; apart is with hasattr(module, &quot;PREVIEW&quot;)?<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; In what situation would you want that when you weren&#39;t referring to a<br>
&gt;&gt; specific module? If you&#39;re referring to a specific module and you<br>
&gt;&gt; really care, just check sys.version. (That&#39;s annoying and ugly enough<br>
&gt;&gt; that it&#39;d probably make you thing about why you are doing it - I<br>
&gt;&gt; cannot honestly think of a case where I&#39;d actually want to check in<br>
&gt;&gt; code if a module is a preview - hence my question as to what your use<br>
&gt;&gt; case is).<br>
&gt;<br>
&gt;<br>
&gt; What&#39;s the use-case for any sort of introspection functionality? I would say that the ability to perform introspection is valuable in and of itself, regardless of any other concrete benefits.<br>
&gt;<br>
&gt; We expect that modules may change APIs between the preview and non-preview (&quot;stable&quot;) releases. I can see value in (say) being forewarned of API changes from the interactive interpreter, without having to troll through documentation looking for changes, or waiting for an exception. Or having to remember exactly which version modules were added in, and when they left preview. (Will this *always* be one release later? I doubt it.)<br>

&gt;<br>
&gt; If you don&#39;t believe that preview modules will change APIs, or that it would be useful to detect this programmatically when using such a module, then there&#39;s probably nothing I can say to convince you otherwise. But I think it will be useful. Python itself has a sys.version so you can detect feature sets and changes in semantics; this is just the same thing, only milder.<br>

&gt;<br>
&gt; The one obvious way[1] is to explicitly tag modules as preview, and the simplest way to do this is with an attribute. (Non-preview modules shouldn&#39;t have the attribute at all -- non-preview is the default state, averaged over the entire lifetime of a module in the standard library.)<br>

&gt;<br>
&gt; It would be just nice to sit down at the interactive interpreter and see whether a module you just imported was preview or not, without having to look it up in the docs. I do nearly everything at the interpreter: I read docs using help(), I check where modules are located using module.__file__. This is just more of the same.<br>

&gt;<br>
&gt; Some alternatives:<br>
&gt;<br>
&gt; 1) Don&#39;t try to detect whether it is a preview module, but use EAFP to detect features that have changed:<br>
&gt;<br>
&gt; try:<br>
&gt;    result = spam.foo(x, y)<br>
&gt; except AttributeError:<br>
&gt;    # Must be a preview release. Do something else.<br>
&gt;    result = spam.bar(y, x)<br>
&gt;<br>
&gt; This is preferred so long as the differences between preview and stable releases are big, obvious changes like a function being renamed. But if there are subtle changes that you care about, things get dicey. spam.foo may not raise an exception, but just do something completely unexpected.<br>

&gt;<br>
&gt;<br>
&gt;<br>
&gt; 2) As you suggest, write version-specific code:<br>
&gt;<br>
&gt; if sys.version &gt;= &quot;3.4&quot;:<br>
&gt;    result = spam.foo(x, y)<br>
&gt; else:<br>
&gt;    # Preview release.<br>
&gt;    result = spam.bar(y, x)<br>
&gt;<br>
&gt;<br>
&gt; This starts to get messy fast, particularly if (worst case, and I *really* hope this doesn&#39;t happen!) modules get put into preview, then get withdrawn, then a few releases later get put back in. This sort of mess shouldn&#39;t ever happen with non-preview modules, but preview modules explicitly have weaker guarantees.<br>

&gt;<br>
&gt; And I can never remember when modules were added to the std lib.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;&gt; Feels like YAGNI to me.<br>
&gt;<br>
&gt;<br>
&gt; When people talk about YAGNI, they are referring to the principle that you shouldn&#39;t waste time and effort over-engineering a complex solution or providing significant additional functionality for no obvious gain. I don&#39;t think that<br>

&gt;<br>
&gt;    PREVIEW = True<br>
&gt;<br>
&gt; in a module *quite* counts as over-engineered.</p>
<p>How about sys.preview_modules to list all the preview modules in the current release?  This would be useful at the interactive prompt, at the least.</p>
<p>-eric</p>