other python ideas

Douglas Alan nessus at mit.edu
Mon Apr 9 04:06:26 EDT 2001


"Jeff Petkau" <jpet at eskimo.com> writes:

> Douglas Alan <nessus at mit.edu> wrote in message

> >    mymodule::foo(a, b, c)

> > This syntax would invoke mymodule.foo(), and would load module
> > "mymodule" it if it wasn't already loaded.  You'd probably also want
> > some sort of module aliasing notation so you could use

> >   alias m my_really_long_named_module
> >   m::foo(a, b, c)

> > in place of

> >   my_really_long_named_module::foo(a, b, c)

> How is this any better than:

>     import my_really_long_named_module as m
>     m.foo(a, b, c)
> ?

Well, because, like I said, I'd like to get rid of import statements
altogether.  A problem with import statements in general is that as
you work on your code over a period of time, you end up with garbage
imports.  I.e., you import stuff that is no longer actually used.
Another problem is that if you import individual objects from a
module, and then you reload the module, the individual objects remain
out of date.  This causes you to end up trying in the debugger to
track down bugs that turn out to debugger-only bugs.

> For avoiding import statements, you could write something
> like this:

>     class ModuleGetter:
>         def __getattr__(self,name):
>             if name.startswith('__'):
>                 raise AttributeError
>                     return __import__(name)
>     module = ModuleGetter()

> And now you can do without the imports:
>     module.mymodule.foo(a, b, c)
>     module.os.chmod(blah, blah)
>     x = module.re.compile('[a-z]')

I could.  But I probably wouldn't like the effect it would have on the
performance of my program if I used it everywhere.  Also, other people
that have to work on my code might end up killing me.

|>oug



More information about the Python-list mailing list