Making classes from Metaclasses globally available

Jacek Generowicz jacek.generowicz at cern.ch
Wed Jun 16 07:13:08 EDT 2004


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_ ?


>>> dir()
[ <stuff> ]
>>> from foo import *
>>> dir(foo)
[ <stuff>, 'hello']
>>> hello()
'Hello, World!'

Here we dynamically inserted variables not known beforehand to the
user, even if they were known beforehand to the module writer.

Not convinced?

OK ...

>>> from treebuilder import build_structure_from_xml_tree
>>> dir()
[ <stuff> ]
>>> build_structure_from_xml_tree('fubar.xml')
>>> dir()
[ <stuff>, 'root' ]
>>> dir(root)
['branch1', 'branch2']


Here we dynamically inserted variables not known beforehand to the
author of the module, which may or may not have been known to the
user. If the user were the author of fubar.xml then, presumably he
would know the variable names; if he were not, then he may well not
know them beforehand. Either way he is likely to want to access them by
name.

... still not convinced ?

I have a program which parses C++ header files and creates Python
proxies for the stuff found therein, allowing you to interact with
objects in C++ libraries. You load something we call a dictionary
(which describes the library) and the program uses that information to
create, dynamically, Python objects with the same names and structures
as the C++ classes in the library.

The author of the program certainly does not know the names of the
objects the program is going to create in the future. The client may
know them (he might be wanting to rewrite some C++ code in Python), or
not (he might be wanting to use Python's introspection capabilities to
poke around the C++ library).



More information about the Python-list mailing list