0

how to generate a call for an objects __doc__ special attribute using a defined variable?

https://code.sololearn.com/c5nWFb40y9BO Trying to work out how to generate a call for an objects __doc__ special attribute by using a defined variable to populate the first part of the call. When I try this the variable name is treated as if it is a string so the docstring for strings comes up. Works okay to call the help info but not the docstring info. Any suggestions? Thanks.

7th Feb 2020, 7:39 PM
Alex
Alex - avatar
7 Réponses
+ 3
Based on all the previous answers, here is a solution, without eval(). https://code.sololearn.com/cx9UKHg7OyTW/?ref=app
7th Feb 2020, 8:58 PM
Tibor Santa
Tibor Santa - avatar
+ 1
def get_some_info(): ui = (input('Type the name of the function you want to find out more about: ')) hds = (input("Type 'ds' for the doc string or 'h' for the help info: ")) if hds == 'ds': print(eval(ui + ".__doc__")) elif hds == 'h': return help(ui) get_some_info()
7th Feb 2020, 8:11 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Thanks ~ swim ~ I shall now go and read about why eval is discouraged. ... and now I know. https://stackoverflow.com/questions/1933451/why-should-exec-and-eval-be-avoided
7th Feb 2020, 8:47 PM
Alex
Alex - avatar
0
This will help me to remember to think of the eval() method. Cheers!
7th Feb 2020, 8:39 PM
Alex
Alex - avatar