python - How to do string with dot operator extension using a variable -


basically trying following code snippet

a = "abc" = dir(a) print print a.__add__ 

the fourth line using variable (in loop) rather operating each time, following (but did not go smooth):

print a.all[0] tmp = a+'.'+all[0] print tmp eval tmp 

please suggest me how can through loop using variable like:

  in :       print a.i 

normally, don't use eval considered unsafe , bad practice overall.

according python docs dir outputs list of strings, representing valid attributes on object

without arguments, return list of names in current local scope. with argument, attempt return list of valid attributes object.

there's built-in method member of class/instance name: getattr (and it's sister methods setattr , hasattr)

a = "qwe"  member in dir(a):     print getattr(a, member) 

prints

<method-wrapper '__add__' of str object @ 0x0000000002ff1688> <class 'str'> <method-wrapper '__contains__' of str object @ 0x0000000002ff1688> <method-wrapper '__delattr__' of str object @ 0x0000000002ff1688> <built-in method __dir__ of str object @ 0x0000000002ff1688> str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -