[Tutor] Assign exec() value to a variable

Mats Wichmann mats at wichmann.us
Thu Oct 10 21:26:12 EDT 2019


On 10/10/19 3:26 PM, Eggo why wrote:
> Hi all,
>     How can I assign result from exec() into a variable.  Following is my code.
> 
> import config
> 
> sid='configdb'
> SID = ("config.%s()" % sid)
> print('--params: ',SID)
> params = exec(SID)
> print('-- params :',params)

exec doesn't return anything (i.e. None), and is not what you want for 
this job - assuming the idea is "call function dynamically".

Instead you can use getattr to look up the function object in the 
module, and then call it. Not promising this actually works since I 
don't have your code:


import config

params = getattr(config, 'configdb')()




More information about the Tutor mailing list