Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'subject:module': 0.04; 'subject:using': 0.04; 'python)': 0.05; 'decent': 0.07; 'interpreter': 0.07; 'present,': 0.07; 'used.': 0.07; 'python': 0.08; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'defined': 0.14; '"from': 0.16; '"import': 0.16; 'caller),': 0.16; 'from:addr:yahoo.com.ar': 0.16; 'importerror': 0.16; 'module).': 0.16; 'overrides': 0.16; 'stub': 0.16; 'subject:import': 0.16; 'compatible': 0.16; 'this:': 0.16; '2.x': 0.19; 'exists.': 0.19; 'importing': 0.19; 'previously': 0.19; 'extension': 0.22; '(on': 0.23; 'versions': 0.23; 'interface': 0.23; 'code': 0.24; '(or': 0.25; 'loaded': 0.25; 'modules': 0.25; 'charset:iso-8859-15': 0.28; 'sat,': 0.28; 'import': 0.29; 'module': 0.30; 'not.': 0.30; 'remains': 0.30; 'successful,': 0.30; 'version': 0.30; 'subject:?': 0.31; 'pure': 0.32; 'does': 0.32; 'to:addr:python-list': 0.34; 'header:X -Complaints-To:1': 0.34; 'header:User-Agent:1': 0.34; 'decide': 0.34; 'there': 0.34; 'which,': 0.35; 'running': 0.35; 'element': 0.37; 'else,': 0.37; 'but': 0.37; 'using': 0.37; 'received:org': 0.38; 'not,': 0.38; 'subject:: ': 0.38; 'think': 0.38; 'perhaps': 0.39; 'should': 0.39; 'header:Mime-Version:1': 0.39; 'unlike': 0.39; 'subject:from': 0.39; 'either': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'your': 0.60; 'here': 0.66; 'alternative': 0.70; '-0300,': 0.84; 'code;': 0.84; 'genellina': 0.84; 'viable': 0.84; 'gabriel': 0.91; 'received:186': 0.91; 'resides': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Gabriel Genellina" Subject: Re: from module import * using __import__? Date: Tue, 05 Jul 2011 01:51:49 -0300 References: Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15; format=flowed; delsp=yes Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 186.19.171.200 User-Agent: Opera Mail/11.11 (Win32) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1309841511 news.xs4all.nl 21815 [2001:888:2000:d::a6]:39059 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8812 En Sat, 02 Jul 2011 16:52:11 -0300, Dan Stromberg escribió: > Is there a decent way of running "from import *"? Perhaps > using > __import__? > > Does it mean using the copy module or adding an element to globals() > somehow? > > Yes, I think I do have a good use for this: importing either pure python > or > cython versions of a module into a single namespace that can provide the > same interface (transparent to the caller), whether C extension modules > are > viable in the current interpreter or not. So you have a stub module that > provides one or the other, depending on availability and suitability. See pickle.py in Python 3 as an example: the slow (pure Python) code resides in pickle.py; the fast (C code) in _pickle.so (on Windows, a built-in module). Near the end of pickle.py, there is a line "from _pickle import *" which, if successful, overrides (replaces) any previous definitions; if not, the ImportError is trapped and the previously defined Python code remains valid. Unlike the 2.x series, where you should decide to "import cPickle" or "import pickle" (or attempt both, cathcing the ImportError), here you only have to "import pickle" in your code; if the fast module is present, it is automatically loaded and used; else, the slow but compatible version is used. You don't even have to know that an alternative implementation exists. -- Gabriel Genellina