Static Modules...

Peter Hansen peter at engcorp.com
Sat Apr 17 15:11:16 EDT 2004


Grzegorz Dostatni wrote:
>>"import this" -> "Explicit is better than implicit."
> 
> This is exactly my point. My way will only work when you reference the
> name explicitly, as in:
> 
> sys.setrecursionlimit(500)
> 
> You're referencing the sys module explicitly. Other than that this
> "problem" exactly parallels the discussion between static and dynamic
> typing. And we know which way python chooses to go.

That's not entirely accurate, I think.  With static typing, you
can't even use a variable if you don't predefine it, but predefining
it doesn't necessarily give it a value (or it gives it a benign
default value).  With dynamic typing, just asking for a variable
doesn't (normally) magically create one with an appropriate value.

This hack is a cool idea for interactive prompts, but in real
code and even at the prompt it could actually be quite "dangerous".
Importing a module can cause arbitrary code to execute, so it
makes some sense to _require_ the import statement.

After all, what if I had a module called, perhaps inappropriately,
"launch" and it triggered the launch of my personal anti-aircraft
missile when imported?  (Yes, bad style too, but I wrote this
control code long ago before I learned good style. ;-)  Now in
one of my other modules, I have a subtle bug** which involves an
object named, perhaps unsurprisingly for this example, "launch".

The code looks like this actually (pulled right out of the source
tree!), slightly edited to preserve national security:

def checkLaunchPermission(self):
     lunch = self.findLaunchController()
     if launch.inhibited:
         # code that doesn't launch anything...

Now if I understand it properly, when your hack is in place
this would actually import the launch module and cause all hell
to break loose.

Did I just make a case for static typing with Python?  Well,
perhaps, but only in the relatively dangerous area of module
imports and there, unlike with simple variable names, Python
already requires explicit (i.e. static) definitions...

-Peter

** Thanks to Greg for starting this discussion as otherwise I
would not have discovered this bug in time to save you all...



More information about the Python-list mailing list