embedding

Michael P. Reilly arcege at shore.net
Fri Aug 6 17:32:47 EDT 1999


Rich Salz <salzr at certco.com> wrote:
: The following (excerpted, no error-checking)
:  PyObject* mod = PyImport_AddModule("__main__");
:  PyObject* mdict = PyModule_GetDict(mod);
:  PyObject* obj = get_a_python_object();
:  PyDict_SetItemString(mdict, "YOUR NAME HERE", pyd);

: This seems simpler than Reilly's method.  So is my code wrong, or (gasp) is
: there More Than One Way To Do It?
:     /r$

More than one way to do it in Python?  "Ni!" to you. ;)  Yes, there is
more than one way to do it.  I don't think it's simplier myself, but
that's just me.

It is the (exact) difference between:
  mod = __import__("__main__")
  setattr(mod, "YOUR NAME HERE", obj)

and:
  mod = __import__("__main__")
  mdict = mod.__dict__
  mdict['YOUR NAME HERE'] = obj

Using the attribute interface is preferable to digging into the
internals.  (I won't get into spaces in namespace names.)

  -Arcege





More information about the Python-list mailing list