[Python-Dev] unexpected import behaviour (original) (raw)
Daniel Waterworth da.waterworth at gmail.com
Sat Jul 31 07:57:33 CEST 2010
- Previous message: [Python-Dev] unexpected import behaviour
- Next message: [Python-Dev] unexpected import behaviour
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 31 July 2010 02:21, Alexander Belopolsky <alexander.belopolsky at gmail.com> wrote:
On Fri, Jul 30, 2010 at 2:46 PM, Daniel Waterworth <da.waterworth at gmail.com> wrote: ..
Having thought it through thoroughly, my preference is for a warning.
I don't think it's a good practise to import the main module by filename, as renaming the file will break the code. I got stung after, having dropped into a python interpreter shell and imported the module, I executed a function that uses isinstance. If a warning showed up after importing the module, explaining the problem and suggested that I use import('main') instead, I would have saved myself a fair amount of time debugging code. This is another case of "Explicit is better than implicit.". You can easily disallow importing main module by filename by simply giving your script a name that does not end with .py or by using say '-' character in the filename. No change to python itself is needed.
My problem is not that I'm likely to fall for the same trap twice. It's that I want to prevent other people from doing so.
@Nick: I suppose the simplest way to detect re-importation in the general case, is to store a set of hashes of files that have been imported. When a user tries to import a file where it's hash is already in the set, a warning is generated. It's simpler than trying to figure out all the different ways that a file can be imported, and will also detect copied files. This is less infrastructure than you were suggesting, but it's not a perfect solution.
Thanks,
Daniel
-- active-thought.com
- Previous message: [Python-Dev] unexpected import behaviour
- Next message: [Python-Dev] unexpected import behaviour
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]