This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2008年11月29日 04:40 by steven.daprano, last changed 2022年04月11日 14:56 by admin. This issue is now closed.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| import-docs.diff | georg.brandl, 2008年12月05日 18:22 | |||
| Messages (6) | |||
|---|---|---|---|
| msg76582 - (view) | Author: Steven D'Aprano (steven.daprano) * (Python committer) | Date: 2008年11月29日 04:40 | |
The documentation for __import__ says that it primarily exists "so that you can replace it with another function that has a compatible interface, in order to change the semantics of the import statement". http://docs.python.org/library/functions.html That is no longer the case. The recommended way to do that is with the new import hooks, and the docs should reflect that. A common use for __import__ is for when you don't know the module name until runtime. That too should be mentioned. |
|||
| msg76637 - (view) | Author: Mart Sõmermaa (mrts) | Date: 2008年11月30日 12:38 | |
Also, the examples that clarify __import__ behaviour by Nick Coghlan should be added: http://mail.python.org/pipermail/python-dev/2008-November/083735.html --- "from foo.bar import baz" ----> <stack top> = __import__('foo.bar', globals(), locals(), ['baz'], -1) baz = <stack top>.baz When there are multiple names being imported or an 'as' clause is involved, I hope the reasons for doing it this way become more obvious: "from foo.bar import baz, bob" ----> <stack top> = __import__('foo.bar', globals(), locals(), ['baz', 'bob'], -1) baz = <stack top>.baz bob = <stack top>.bob "from foo.bar import baz as bob" ----> <stack top> = __import__('foo.bar', globals(), locals(), ['baz', 'bob'], -1) bob = <stack top>.baz --- And the "winning idiom" by Hrvoje Niksic for accessing module 'z', given name hierarchy 'x.y.z' should be documented as well: >>> import sys >>> __import__('x.y.z') >>> mod = sys.modules['x.y.z'] |
|||
| msg77051 - (view) | Author: Georg Brandl (georg.brandl) * (Python committer) | Date: 2008年12月05日 18:22 | |
Attached is a proposed rewrite of the __import__ documentation. |
|||
| msg77052 - (view) | Author: Mart Sõmermaa (mrts) | Date: 2008年12月05日 18:25 | |
Brett, don't you think the
>>> import sys
>>> __import__('x.y.z')
>>> mod = sys.modules['x.y.z']
idiom should be recommended instead of/additionally to the lengthy
getattr() one?
|
|||
| msg77056 - (view) | Author: Brett Cannon (brett.cannon) * (Python committer) | Date: 2008年12月05日 19:19 | |
Yes, the simple __import__/sys.modules idiom should be the suggested idiom until a proper function is provided in the standard library. |
|||
| msg77272 - (view) | Author: Georg Brandl (georg.brandl) * (Python committer) | Date: 2008年12月07日 22:42 | |
Changed patch to document sys.modules trick and applied in r67654. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:56:41 | admin | set | github: 48707 |
| 2008年12月07日 22:42:20 | georg.brandl | set | status: open -> closed resolution: fixed messages: + msg77272 |
| 2008年12月05日 19:19:56 | brett.cannon | set | nosy:
+ brett.cannon messages: + msg77056 |
| 2008年12月05日 18:25:23 | mrts | set | messages: + msg77052 |
| 2008年12月05日 18:22:26 | georg.brandl | set | files:
+ import-docs.diff keywords: + patch messages: + msg77051 |
| 2008年11月30日 12:38:21 | mrts | set | nosy:
+ mrts messages: + msg76637 |
| 2008年11月29日 04:40:53 | steven.daprano | create | |