From: Guy K. K. <g....@ma...> - 2010-10-08 23:07:41
|
On Sat, 09 Oct 2010 10:44:29 Bruce Sherwood wrote: > This proposal assumed that the "libvisual" file was in the visual > folder, in which case I thought that relative imports were now > favored, for various reasons. As I understand it, the main issue is > that there could be a module named "foo" at a higher level in the > search path, in which case "import foo" might not pick up our foo, but > someone else's foo. Have I misunderstood something? Well, not really. It's just that the use of relative imports is mainly intended for corner cases, but it's discouraged to be used on a general basis. Of course, things could break if there's a clash between two "foo" modules. This is called "shadowing". One common case is for example when people call a variable "file" and therefore are shadowing the built-in "file" type. In other cases, this behaviour is exactly desired, so for example when people *want* to use a specific implementation as a drop-in replacement for an otherwise used package. This is for example quite common with the XML parser through the ElementTree API. Many APIs are designed to be compatible with each other, another one of those are the ones of the threading/processing modules. So a user can choose which one to use (for various reasons). So what it boils down to is to commonly use absolute imports rather than relative ones, unless something demands a relative import for specific reasons. From the Python documentation [1]: "Relative imports can lead to a module being initialized twice, leading to confusing bugs. See PEP 328 [2] for details." You can read up more on this and related issues more on [1], [2] and [3]. Guy [1] http://docs.python.org/faq/programming.html#what-are-the-best-practices- for-using-import-in-a-module [2] http://docs.python.org/release/2.5/whatsnew/pep-328.html [3] http://bugs.python.org/issue10031 Guy -- Guy K. Kloss Institute of Information and Mathematical Sciences Te Kura Pūtaiao o Mōhiohio me Pāngarau Massey University, Albany (North Shore City, Auckland) 473 State Highway 17, Gate 1, Mailroom, Quad B Building voice: +64 9 414-0800 ext. 9266 fax: +64 9 441-8181 G....@ma... http://www.massey.ac.nz/~gkloss |