[capi-sig] is there a tp_getattr equivalent for module?

Hrvoje Niksic hniksic at xemacs.org
Fri Dec 7 15:33:16 CET 2007


"Anton Tropashko" <sndive at gmail.com> writes:

> is there a tp_getattr equivalent for module?  i want to dynamically
> check if an object exists in a module space dynamically and return
> it if it does.

You don't need to call tp_getattr yourself, PyObject_GetAttr will do
it for you.  If you need to check whether an object exists in a module
without accessing it using mymodule.name, simply use getattr on the
module object like you'd do with any other object.  For example:

import mymodule
name = 'foo'
if hasattr(mymodule, name):
  print 'mymodule has', name, 'and its value is', getattr(mymodule, name)
else:
  print 'no', name, 'in mymodule'

Equivalent C code would use PyObject_HasAttr/PyObject_GetAttr or its
*String variants.


More information about the capi-sig mailing list