Howto: creating empty modules?

Janko Hauser jhauser at ifm.uni-kiel.de
Wed Jan 9 11:05:54 EST 2002


"Alex Martelli" <aleax at aleax.it> writes:

> It's OK to insert this new module in sys.modules:
> 
> import sys
> sys.modules.append(config)
> 
> and then any other module can get at it with
> 
> import config.
> 
> 
So, I'm mistaken :-(.  

> > If he just wants to have a place to store configuration in the calling
> module
> > an empty class would be enough.
> >
> > class EmptyClass:
> >     pass
> >
> > config=EmptyClass()
> > config.someattr = 'hi'
> >
> > For variations on this theme (singleton) look for posts from Alex
> > Martelli :-).
> 
> A class gives peculiar problems and no advantages for the use you
> surmise.  E.g., you can't just have one of the class's attributes
> be a function -- you have to use the staticmethod built-in (in 2.2,
> or build the equivalent yourself in previous releases).  If one is
> using a Python release older than 2.1, a class or an instance
> cannot be registered in sys.modules (it's OK from 2.1 onwards).
> 
Had not thought about functions. Basically this amounts to a a global
namespace (sys.modules) from which one explicitly imports new
namespaces, modules or classes. Are there many common uses of this
namespace handling/changing?


> All in all, if one only wants to do stuff a module can do well, I
> think it's better to use a module for this purpose, rather than a
> class (or an instance or other things yet).  MHO, of course.
> 
Thanks for this HO.

__Janko


-- 
  Institut fuer Meereskunde             phone: 49-431-597 3989
  Dept. Theoretical Oceanography        fax  : 49-431-565876
  Duesternbrooker Weg 20                email: jhauser at ifm.uni-kiel.de
  24105 Kiel, Germany



More information about the Python-list mailing list