Python Forum

Full Version: exec in a function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I'm modifying a code by moving an "exec" inside a function creating an error; I wrote a small code highlighting what I'm facing.

What I'm missing and what is the correct programming ?

Thanks

What I'm missing

Sentence = "World is "

print("without using a function")
Add0 = 'beautifull'
exec("New0 = Sentence + '%s'" %Add0)
print("\t- Complete sentence 0 = {}".format(New0))

print("\nUsing a function")
def Complete(Part):
    Add = 'beautifull'
    exec("New = Part + '%s'" %Add)
    print("\t- Complete sentence 1 = {}".format(New))
    print("\t- Complete sentence 2 = %s" %(New))
    
Complete(Sentence)
You should include the error in your post
Error:
NameError: name 'New' is not defined
New is not the same as New0
(Apr-18-2021, 07:48 PM)Yoriz Wrote: [ -> ]You should include the error in your post
you're right, I'll do next time

I'm trying to understand why outside the function exec works (with New0) but not inside (with New that has no relation with New0); I'm missing things but I've not understood what so far
Sorry, i didn't notice the exec("New = Part + '%s'" %Add).

See this link

Sentence = "World is "
 
print("without using a function")
Add0 = 'beautifull'
exec("New0 = Sentence + '%s'" %Add0)
print("\t- Complete sentence 0 = {}".format(New0))
 
print("\nUsing a function")
def Complete(Part):
    Add = 'beautifull'
    ldict = locals()
    exec("New = Part + '%s'" %Add,globals(),ldict)
    New = ldict['New']
    print("\t- Complete sentence 1 = {}".format(New))
    print("\t- Complete sentence 2 = %s" %(New))
     
Complete(Sentence)
Output:
without using a function - Complete sentence 0 = World is beautifull Using a function - Complete sentence 1 = World is beautifull - Complete sentence 2 = World is beautifull
Thanks Yoriz for pointing out the issue.

Another solution seems to use globals() function as a dictionnary (key + value)

Sentence = "World is "
 
print("without using a function")
Add0 = 'beautifull'
exec("New0 = Sentence + '%s'" %Add0)
print("\t- Complete sentence 0 = {}".format(New0))
 
print("\nUsing a function")
def Complete(Part):
    Add = 'beautifull'
    globals()["New"] = Part + Add
    print("\t- Complete sentence 1 = {}".format(New))
    print("\t- Complete sentence 2 = %s" %(New))
     
Complete(Sentence)
Output:
without using a function - Complete sentence 0 = World is beautifull Using a function - Complete sentence 1 = World is beautifull - Complete sentence 2 = World is beautifull
Why do you want to use exec in the first place? Writing code in strings that you glue together just makes it harder to understand the program. Sure, there are likely cases where you do need exec, but I think those are quite rare.
(Apr-19-2021, 07:59 AM)ndc85430 Wrote: [ -> ]Why do you want to use exec in the first place?

It's a basic code especially created to highlight the issue I was confronted to Wink