[Libreoffice] candidate for a new python section in SDK examples page

Lionel Elie Mamane lionel at mamane.lu
Wed Dec 7 09:05:56 PST 2011


On Wed, Dec 07, 2011 at 03:49:10PM +0000, Michael Meeks wrote:

> Any python genius that can tell me how:

I'm not sure I qualify as "genius", but let me try.

> 	mod = __import__ ( "actual python-module-name" )
>       implHelper = mod.__dict__.get( "g_ImplementationHelper" , None )

> 	is supposed to work, or indeed any half-way decent documentation on
> what the object is that __import__ returns

http://docs.python.org/reference/datamodel.html and Python's source
code (Objects/moduleobject.c):

It is an object of type/class "module". Type module has exactly one
member:

__dict__ (read-only): the module's namespace as a dictionary
  (http://docs.python.org/library/stdtypes.html#typesmapping)

  __dict__ is the dictionary of attributes of the object. That is, if
  there is no member BAR in object FOO, then FOO.BAR is
  FOO.__dict__["BAR"]

__dict__ is prepopulated by the module loading system with:

_name__ is the module’s name

__doc__ is the module’s documentation string, or None if unavailable

__file__ is the pathname of the file from which the module was loaded,
  if it was loaded from a file. The __file__ attribute is not present
  for C modules that are statically linked into the interpreter; for
  extension modules loaded dynamically from a shared library, it is the
  pathname of the shared library file.

But they are read/write and can be freely changed by Python code.

> let us find an internal variable definition ;-)

The code you quoted looks right. I would then continue with:

 if inspect.isroutine(implHelper):

to test whether implHelper is a function (something that takes
arguments and returns a result) as I guess it is expected to be. The
inspect module having been loaded, obviously.

-- 
Lionel


More information about the LibreOffice mailing list