Normally, you don't use eval
as it is considered unsafe and bad practice overall.
According to python docs dir
outputs list of strings, representing valid attributes on the object
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
There's a built-in method to get a member of class/instance by name: getattr
(and it's sister methods setattr
and hasattr
)
a = "QWE"
for member in dir(a):
print getattr(a, member)
Prints
<method-wrapper '__add__' of str object at 0x0000000002FF1688>
<class 'str'>
<method-wrapper '__contains__' of str object at 0x0000000002FF1688>
<method-wrapper '__delattr__' of str object at 0x0000000002FF1688>
<built-in method __dir__ of str object at 0x0000000002FF1688>
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str