[Tutor] Assign exec() value to a variable

Eggo why eggo at hotmail.com
Thu Oct 10 21:42:40 EDT 2019


Hi Mats,
     Thank you very much.  It works the way I wanted.

Gary
________________________________
From: Tutor <tutor-bounces+eggo=hotmail.com at python.org> on behalf of Mats Wichmann <mats at wichmann.us>
Sent: Friday, October 11, 2019 1:26 AM
To: tutor at python.org <tutor at python.org>
Subject: Re: [Tutor] Assign exec() value to a variable

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')()


_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list