locals() is a dict. If I do a simple:
for i in locals().keys():
type(i)
…what’s returned is that the names are all strings (yes, in point of fact, they are) but those keys indicate different types – one might be, say, a module. Some reference types (objects) but also primitive types (e.g. int, string).
In some sense, I want to extract the name from the string. Looked at how to do this with the attributes on a string but no go. I believe I need something from introspection, but what?
locals().keys()returns all the variable names bound in local space. Since they are names, they are all strings. (In Python 2.x you could uselocals().iterkeys()for this also, to get an iterator.)Try
locals().values()instead. For Python 2.x, you can uselocals().itervalues()to get an iterator.Or
locals().items()to getkey, valuepairs. For Python 2.x you can uselocals().iteritems()to get an iterator.