Making classes from Metaclasses globally available

Jacek Generowicz jacek.generowicz at cern.ch
Wed Jun 16 15:55:15 CEST 2004


Peter Otten <__peter__ at web.de> writes:

> Jacek Generowicz wrote:
> 
> > Peter Otten <__peter__ at web.de> writes:
> > 
> >> Note, however, that dynamically inserting variables is not the best
> >> programming practice. When you don't know the variable name in
> >> advance, what would be the benefit of being able to access the
> >> object via its identifier?
> > 
> > When _who_ doesn't know the variable name in advance of _what_ ?
> 
> I may be fighting with the english language here - and lose...

Don't think so; I was merely trying to point out that "you" can have
different meanings in this context (program author, program client),
and that the same is true of "in advance" (before the program is
written, before the program is run).

> I've taken to avoid the * import,

Yes, I advise my Python students not to do it ... however a there are
some standard modules which are designed to be imported like this.

> - but why can't your function_with_a_long_name() just return the root
> *object* and let the user decide about the appropriate name

This only works if your objects share a single common root.

To be honest, that is exactly what my functions of this type do
(though in one case I would like to try having a third namespace
similar to globals and builtins where to shove all this stuff
... unfortunately I haven't found a satisfactory way of creating one
yet.)

Still, what you are doing is programatically adding attributes to some
namespace ... and maybe the user should have the option of dumping
those attributes in the global namespace, just like he does with
"import".

> The recommendation to avoid dynamic manipulations of the global namespace
> was not meant as a rule carved in stone, but rather a means to write
> cleaner code and avoid errors that may be hard to reproduce.

You asked 

  When you don't know the variable name in advance, what would be the
  benefit of being able to access the object via its identifier?

I merely took your question literally (even though it was probably
rhetorical) and offered examples of such use cases (probably
rhetorically :-).

In summary, the answer is:

  Just because the author of the code does not know the the name at
  the time he writes the code, does not mean that the code's client
  cannot know the name before he runs the code.



More information about the Python-list mailing list