Namespaces. Python’s approach to imports is possibly my favourite feature of the language. I love being able to scan up to the top of a file in my text editor and see exactly where every symbol comes from, no IDE required.
Namespaces. Python’s approach to imports is possibly my favourite feature of the language. I love being able to scan up to the top of a file in my text editor and see exactly where every symbol comes from, no IDE required.
Though in Python code it's possible (if generally bad form) to have import statements all over the file itself. Which make it much more difficult to know where symbols come from (and make me stabby)
masklinn - 2nd December 2009 12:54 - #
This fits in quite nicely with the Python tenet that bad behaviour should be discouraged, but not outright banned. For instance, there are just a couple of cases where mid-file imports would seem like a good idea. For instance, when debugging you might want to insert into your code:
This means you can just delete those two lines once you're done debugging. It also gives you the freedom to import as a try...except block, which comes in handy when useful functions get moved around between releases (
parse_qs, I'm looking at you).Of course, when people just sprinkle imports all over their code, please feel free to stab them. This counts as a very effective form of discouragement.
yeah... until you notice that if a module you import imports another module and you've imported it's parent you can use it without importing it.
... Don't do this.
Gavin - 2nd December 2009 15:32 - #
Yes, don't put import statements in functions, for example. Even though they shouldn't have an effect after the module in question has been imported, at least conceptually, there's actually quite a cost, as a profiling session once indicated to me.
Paul Boddie - 4th December 2009 00:49 - #