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.
7 Réponses
+ 3
Based on all the previous answers, here is a solution, without eval().
https://code.sololearn.com/cx9UKHg7OyTW/?ref=app
+ 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()
+ 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
0
This will help me to remember to think of the eval() method. Cheers!