7 Dec
2007
7 Dec
'07
2:33 p.m.
"Anton Tropashko" sndive@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.