A desperate lunge for on-topic-ness

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Oct 24 00:34:23 CEST 2012


On Tue, 23 Oct 2012 10:50:11 -0600, Ian Kelly wrote:

>> if someone is foolish enough to use the
>>
>>         from xyz import *
>>
>> notation...
> 
> It's already a SyntaxError to use a wildcard import anywhere other than
> the module level, so its use can only affect global variables.

In Python 3.x. 

In Python 2.x, which includes the most recent version of three of the 
four "big implementations" (PyPy, Jython, IronPython) it is still legal, 
at least in theory.

I haven't tested PyPy, but IronPython 2.6 allows wildcard imports inside 
functions without even a warning. Bizarrely, Jython 2.5 *appears* to 
allow them with only a warning, but they don't take:

steve at runes:~$ jython
Jython 2.5.1+ (Release_2_5_1, Aug 4 2010, 07:18:19)
[OpenJDK Client VM (Sun Microsystems Inc.)] on java1.6.0_18
Type "help", "copyright", "credits" or "license" for more information.
>>> def test():
...     from math import *
...     return cos
...
<stdin>:2: SyntaxWarning: import * only allowed at module level
>>> test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in test
NameError: global name 'cos' is not defined


So, legal or not, they're definitely something you want to avoid.


-- 
Steven


More information about the Python-list mailing list